使用GreaseMonkey和jQuery从href收集title属性以放入div

时间:2015-04-18 01:03:20

标签: javascript jquery greasemonkey

我一直在尝试使用GreaseMonkey和jQuery从我们在工作中使用的门户收集title。 jsFiddle示例很高兴看到代码但对我没有多大帮助:http://jsfiddle.net/Twisty/gmoz6r7j/

GreaseMonkey代码

// ==UserScript==
// @name        BOR Collect Block Data
// @require     https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
// @namespace   https://xx.xx.xx.xx/cgi-bin/bor/bl.pl
// @description Collects the Block data and displays in a Dialog for ease of viewing.
// @include     https://xx.xx.xx.xx/cgi-bin/bor/bl.pl
// @version     1
// @grant       none
// ==/UserScript==
$('a').click(function (e) {
    e.preventDefault();
    var title = $(this).attr('title');
    if (title.length) {
        console.log('== \'title\' collected:');
        console.log(title);
    }
    var dataLines = title.split(/\n/);
    if (dataLines.length) {
        console.log('== \'dataLines\' parsed:');
        console.log(dataLines);
    } else {
        colsole.log('dataLines failed to create!');
    }
    var lines = {};
    $.each(dataLines, function (val) {
        var sp = val.split(/:(.+)?/);
                var k = sp[0].trim();
                var v = sp[1].trim();
        lines[k] = v;
    });
    if (lines.length) {
        console.log('== \'lines\' parsed:');
        console.log(lines);
    } else {
        console.log('Lines failed to create!');
        return;
    }
    var results = $('<ul>');
    results.css('margin', 0).css('padding', 0);
    $.each(lines, function (key, val) {
        results.append('<li style=\'list-style: none\'><label style=\'display: inline-block; width: 120px;\'>' + key + '</label>' + val + '</li>');
    });
    if ($('#blockData').lentgh) {
        console.log('Dialog DIV already found.');
        dialog = $('#blockData');
    } else {
        console.log('Dialog DIV being created.');
        dialog = $('<div>');
        dialog.attr('id', 'blockData');
    }
    dialog.css('float', right).css('top', 0);
    console.log('Adding results.');
    console.log(results);
    dialog.html(results);
    $('body').prepend(dialog);
});

以下是门户网站提供的 HTML 的示例:

<body bgproperties="fixed">
    <br>
    <table border=0 width=99%>
        <TR valign=middle>
            <TD align=center width=10>&nbsp;</td>
            <TD align=left width=15%></TD>
            <TD align=center width=170>&nbsp;Your IP Address is
                <br>10.16.194.81</TD>
            <TD align=right width=24%>
                <br>&nbsp;</TD>
        </TR>
    </table>
    <br>
    <table>
        <br>
        <table border=1 cellpadding=2>
            <tr>
                <td><a href="JavaScript:void window.open ('/cgi-bin/bor/ipinfo.pl?ip=1.0.130.120', 'newwindow', config='height=800, width=500, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no')" title="Used username to attack:      0633: FTP: BAD LOGIN 
Attack against:              xx.xx.xx.xx 
Initiator :              SCRIPT 
What kind :              TP3MPT 
Block date :             2015-04-15 20:02:11 
Unblock date :           2015-04-23 19:05:11 ">1.0.130.120</a>

                </td>
                <td><a href="JavaScript:void window.open ('/cgi-bin/bor/ipinfo.pl?ip=1.0.130.215', 'newwindow', config='height=800, width=500, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no')" title="Used username to attack:      4141: FTP: ATTEMPTED ROOT/ADMINISTRATOR LOGIN 
Attack against:              xx.xx.xx.xx 
Initiator :              SCRIPT 
What kind :              TP2TWW 
Block date :             2015-04-11 20:27:53 
Unblock date :           2015-04-19 01:31:53 ">1.0.130.215</a>

                </td>
            </tr>
        </table>

当我点击某个链接时,我想覆盖WHOIS查找,从该A收集标题并将其放入具有一些不错格式的DIV中。

当我使用GreaseMonkey在FireFox中运行它时,我在控制台中得到以下结果:

== 'title' collected:    
Used username to attack:     4141: FTP: ATTEMPTED ROOT/ADMINISTRATOR LOGIN 
Attack against:              xxx.xxx.xxx.xxx
Initiator :              SCRIPT 
What kind :              TP3MPT 
Block date :             2015-04-15 18:54:58 
Unblock date :           2015-04-23 17:30:58 

== 'dataLines' parsed:
["Used username to attack:...OT/ADMINISTRATOR LOGIN ", "Attack against:              xxx.xxx.xxx.xxx ", "Initiator :             SCRIPT ", "What kind :              TP3MPT ", "Block date :             2015-04-15 18:54:58 ", "Unblock date :              2015-04-23 17:30:58 "]

我一直无法弄清楚脚本为什么不继续。我已经检查了验证,现在我很困惑。我只需要朝着正确的方向努力。

1 个答案:

答案 0 :(得分:0)

我有几件事需要解决。最终工作:

$('a').click(function (e) {
    e.preventDefault();
    var title = $(this).attr('title');
    if (title.length) {
        console.log('== \'title\' collected:');
        //console.log(title);
    }
    var dataLines = title.split(/\n/);
    if (dataLines.length) {
        console.log('== \'dataLines\' parsed:');
        //console.log(dataLines);
    } else {
        colsole.log('dataLines failed to create!');
    }
    lines = {};
    $.each(dataLines, function (key, val) {
        //console.log("Splitting Line: " + val);
        var sp = val.split(/:(.+)?/);
                if(sp.length){
                  console.log("Line: { " + sp[0].trim() +" : '" + sp[1].trim() + "' }");
                    lines[sp[0].trim()] = sp[1].trim();
                } else {
                  console.log("Split failed.");
                    return;
                }
    });
    if (Object.keys(lines).length) {
        console.log('== \'lines\' parsed:');
        //console.log(lines);
    } else {
        console.log('Lines failed to create!');
    }
    var results = $('<ul>');
    results.css('margin', 0).css('padding', 0);
    $.each(lines, function (key, val) {
        results.append('<li style=\'list-style: none\'><label style=\'display: inline-block; width: 150px;\'>' + key + '</label>' + val + '</li>');
    });
        if(results.length){
          console.log(results);
        } else {
          console.log("No \'results\'.");
        }
    if ($('#blockData').length) {
        console.log('Dialog DIV already found.');
        var dialog = $("#blockData");
    } else {
        console.log('Dialog DIV being created.');
        var dialog = $("<div>");
        dialog.attr('id', 'blockData');
    }
        if(dialog.length){
      dialog.css('float', 'right')
              .css('top','0')
                .css('display', 'inline-block')
                .css('width', '480px')
                .css('margin', '10px');
            console.log(dialog);
      console.log('Adding results.');
      dialog.html(results);
        } else {
          console.log("DIV creation failed.");
        }
    $('body').prepend(dialog);
});