mcfearsome (owner)

Revisions

gist: 9285 Download_button fork
public
Description:
Ubiquity command to show you your current ip address.
Public Clone URL: git://gist.github.com/9285.git
whatsmyip.ubiq.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// ==Ubiquity==
// @name Whats My IP
// @description Copies your IP to your clipboard. Also shows your IP as a preview. The IP is retrieved from http://www.ipchicken.com
// ==/Ubiquity==
 
 
if(typeof(McFearsome) == 'undefined') {
  McFearsome = {};
}
jQuery.extend(McFearsome, {
  whatsmyip: {
    utils: {
      save_ip: function( ip ) {
        // CmdUtils.log("Saving your ip. " + ip);
        Application.prefs.setValue('ubiquity.mcfearsome.whatsmyip.ip', ip);
      },
      get_ip: function() {
        return Application.prefs.getValue('ubiquity.mcfearsome.whatsmyip.ip', false);
      },
      clear_ip: function() {
        try {
          Application.prefs.get('ubiquity.mcfearsome.whatsmyip.ip').reset();
        } catch(e) {
          // CmdUtils.log(e);
        }
      },
      save_to_clipboard: function() {
        var ip = McFearsome.whatsmyip.utils.get_ip();
        try {
          if(ip) {
            Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper).copyString(ip);
            displayMessage("Your IP, \"" + ip + "\" has been copied to your clipboard.");
          } else {
            throw "No IP address found.";
          }
        } catch(e) {
          displayMessage("There was a problem trying to save the IP to your clipboard.");
          displayMessage(e);
        } finally {
          McFearsome.whatsmyip.utils.clear_ip();
        }
      },
      remote_get_ip: function() {
        var ip = null;
        var ip_pattern = /\b((?:\d{1,3}\.){3}\d{1,3})\b/;
        var html = jQuery.ajax({
          type: "GET",
          url: "http://www.ipchicken.com",
          async: false }).responseText;
          
        var result = html.match(ip_pattern);
        if(result) {
          McFearsome.whatsmyip.utils.save_ip(result[0]);
          ip = result[0];
        }
         
        return ip;
      }
    }
  }
});
 
CmdUtils.CreateCommand({
  name: "whats-my-ip",
  icon: "http://www.ipchicken.com/images/ipc.gif",
  description: "Copies your IP to the clipboard.",
  help: "Copies your IP to your clipboard. Also shows your IP as a preview. The IP is retrieved from http://www.ipchicken.com",
  homepage: "http://blog.mcfearsome.com/verbs/",
  author: { name: "Jesse McPherson", email: "jesse@mcfearsome.com"},
  preview: function( previewBlock ) {
    previewBlock.innerHTML = "Getting your IP...";
    previewBlock.innerHTML = "Your IP is: " + McFearsome.whatsmyip.utils.remote_get_ip();
  },
  execute: function( ubiqInput ) {
    McFearsome.whatsmyip.utils.save_to_clipboard();
  }
});