How can you find all the IP addresses in a selected block of text with a javascript bookmarklet?

StackOverflow https://stackoverflow.com/questions/72921

Question

I am just starting to learn javascript, so I don't have the skills to figure out what I assume is a trivial problem.

I'm working with a Wordpress blog that serves as a FAQ for our community and I am trying to pull together some tools to make managing the comments easier. Internet Duct Tape's Greasemonkey tools, like Comment Ninja, are helpful for most of it, but I want to be able to get a list of all the IP addresses that we're getting comments from in order to track trends and so forth.

I just want to be able to select a bunch of text on the comments page and click a bookmarklet (http://bookmarklets.com) in Firefox that pops up a window listing all the IP addresses found in the selection.

Update:

I kind of combined a the answers from levik and Jacob to come up with this:

javascript:ipAddresses=document.getSelection().match(/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/g).join("<br>");newWindow=window.open('', 'IP Addresses in Selection', 'innerWidth=200,innerHeight=300,scrollbars');newWindow.document.write(ipAddresses)

The difference is that instead of an alert message, as in levik's answer, I open a new window similar to Jacob's answer. The alert doesn't provide scroll bars which can be a problem for pages with many IP addresses. However, I needed the list to be vertical, unlike Jacob's solution, so I used the hint from levik's to make a
for the join instead of levik's \n.

Thanks for all the help, guys.

Was it helpful?

Solution

In Firefox, you could do something like this:

javascript:alert(
  document.getSelection().match(/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/g)
  .join("\n"))

How this works:

  • Gets the selection text from the browser ("document.getSelection()" in FF, in IE it would be "document.selection.createRange().text")
  • Applies a regular expression to march the IP addresses (as suggested by Muerr) - this results in an array of strings.
  • Joins this array into one string separated by return characters
  • Alerts that string

The way you get the selection is a little different on IE, but the principle is the same. To get it to be cross-browser, you'd need to check which method is available. You could also do more complicated output (like create a floating DIV and insert all the IPs into it).

OTHER TIPS

Use a regular expression to detect the IP address. A couple examples:

/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/
/^([1-9][0-9]{0,2})+\.([1-9][0-9]{0,2})+\.([1-9][0-9]{0,2})+\.([1-9][0-9]{0,2})+$/

As a bookmarklet

javascript:document.write(document.getSelection().match(/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/g))

Just create a new bookmark and paste that javascript in

How to do it in Ubiquity

CmdUtils.CreateCommand({
    name: "findip",
    preview: function( pblock ) {
        var msg = 'IP Addresses Found<br/><br/> ';
        ips = CmdUtils.getHtmlSelection().match(/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/g);
        if(ips){
            msg += ips.join("<br/>\n");
        }else{
            msg += 'None';
        }
        pblock.innerHTML = msg;
    },

    execute: function() {
        ips = CmdUtils.getHtmlSelection().match(/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/g);
        if(ips){
            CmdUtils.setSelection(ips.join("<br/>\n"));
        }
    }
})

Here is a good article on obtaining the IP address of your visitors. You could display this in addition to their comment if you wanted or include it as a label or field in your page so you can reference it later.

Have a look at the rot13 bookmarklet for an example of selecting text and performing an action (in this case substitution) when the bookmarklet is clicked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top