home *** CD-ROM | disk | FTP | other *** search
/ PC User 2004 November / PCU1104CD1.iso / software / security / files / spoofire.xpi / chrome / spoofstick.jar / content / spoofstick.js < prev    next >
Encoding:
Text File  |  2004-05-16  |  6.2 KB  |  244 lines

  1. /*
  2.  * SpoofStick - Main Extension Code
  3.  *
  4.  * Copyright (C) 2004 CoreStreet, Ltd.
  5.  * http://www.corestreet.com/
  6.  *
  7.  * Author: Mark Ayzenshtat
  8.  */
  9.  
  10. const targetID = "spoofstick-desc";
  11. const youAreOnID = "spoofstick-youareon";
  12. const optionsButtonID = "spoofstick-options";
  13. const labelAttr = "value";
  14. const notVisibleAttr = "collapsed";
  15.  
  16. const prefsClassString = "@mozilla.org/preferences-service;1";
  17. const branchName = "spoofstick.";
  18. const showYouAreOnPref = "showYouAreOn";
  19. const showOptionsButtonPref = "showOptionsButton";
  20. const displaySizePref = "size";
  21. const size1Pref = "size1";
  22. const size2Pref = "size2";
  23. const size3Pref = "size3";
  24. const displayColorPref = "color";
  25. const showFullHostNamePref = "showFullHostName";
  26.  
  27. const optionsWindowURI = "chrome://spoofstick/content/options.xul";
  28. const optionsWindowTitle = "SpoofStick Options";
  29. const optionsWindowParams = "chrome,centerscreen";
  30.  
  31. const aboutWindowURI = "chrome://spoofstick/content/about.xul";
  32. const aboutWindowTitle = "About SpoofStick";
  33. const aboutWindowParams = "chrome,centerscreen";
  34.  
  35. // basic regular expression for IP addresses
  36. const ipPattern = /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/;
  37.  
  38. const fileProtocolText = "(local filesystem)";
  39.  
  40. var commonTLDs = ["com", "org", "net", "edu", "gov", "mil"];
  41.  
  42. var showYouAreOn;
  43. var showOptionsButton;
  44. var displaySize;
  45. var displayColor;
  46. var showFullHostName;
  47.  
  48. // preferences-related objects
  49. var prefs = Components.classes[prefsClassString].
  50.     getService(Components.interfaces.nsIPrefService);        
  51. var prefsBranch = prefs.getBranch(branchName);
  52.  
  53. function updateSpoofStick(eventOrPage) {
  54.     var page = getPage(eventOrPage);
  55.     
  56.     // reload options every time we update so that
  57.     // users don't have to restart their browsers
  58.     reloadOptions();
  59.     
  60.     updateControls();
  61.     
  62.     var ssTarget = document.getElementById(targetID);    
  63.     ssTarget.setAttribute(labelAttr, getDomainName(page));
  64. }
  65.  
  66. function clearSpoofStick() {
  67.     var ssTarget = document.getElementById(targetID);    
  68.     ssTarget.setAttribute(labelAttr, "");
  69. }
  70.  
  71. function updateControls() {
  72.     updateShowYouAreOn();
  73.     updateShowOptionsButton();
  74.     updateDisplay();
  75. }
  76.  
  77. function updateShowYouAreOn() {    
  78.     document.getElementById(youAreOnID).setAttribute(notVisibleAttr, !showYouAreOn);
  79. }
  80.  
  81. function updateShowOptionsButton() {
  82.     document.getElementById(optionsButtonID).setAttribute(notVisibleAttr, !showOptionsButton);
  83. }
  84.  
  85. function updateDisplay() {
  86.     var ssTarget = document.getElementById(targetID);
  87.     var youAreOn = document.getElementById(youAreOnID);
  88.     
  89.     // update color
  90.     ssTarget.setAttribute("style", "color:" + displayColor + ";");    
  91.     
  92.     // update size
  93.     ssTarget.setAttribute("class", "size" + displaySize);
  94.     youAreOn.setAttribute("class", "size" + displaySize);
  95. }
  96.  
  97. function getPage(eventOrPage) {
  98.     if (eventOrPage.type) {
  99.         return eventOrPage.target.contentDocument;
  100.     } else {
  101.         return eventOrPage;
  102.     }
  103. }
  104.  
  105. function getDomainName(p) {
  106.     if (p.URL.indexOf("file:") == 0) {
  107.         // user is browsing their local filesystem
  108.         return fileProtocolText;
  109.     }
  110.     
  111.     if (showFullHostName) {
  112.         return p.location.hostname;
  113.     }
  114.     
  115.     // otherwise, show just the domain name
  116.     
  117.     var rawDomain = p.domain;
  118.     
  119.     var firstDot = rawDomain.indexOf(".");
  120.     var lastDot = rawDomain.lastIndexOf(".");
  121.     
  122.     if (firstDot == lastDot) {
  123.         return rawDomain;
  124.     }
  125.     
  126.     var dot2FromEnd = rawDomain.lastIndexOf(".", lastDot - 1);
  127.     var dot3FromEnd = rawDomain.lastIndexOf(".", dot2FromEnd - 1);
  128.     
  129.     // if the domain has a common TLD like com, net, org, etc.
  130.     // or if it only has 3 tokens, return the last 2
  131.     var tld = rawDomain.substring(lastDot + 1, rawDomain.length);
  132.     if (dot3FromEnd == -1 || isCommonTLD(tld)) {
  133.         return rawDomain.substring(dot2FromEnd + 1, rawDomain.length);
  134.     }
  135.     
  136.     // if the domain name is an IP address, return it whole
  137.     if (isIPAddress(rawDomain)) {        
  138.         return rawDomain;
  139.     }
  140.     
  141.     // assume some other TLD with at least 4 tokens -
  142.     // return the last 3
  143.     return rawDomain.substring(dot3FromEnd + 1, rawDomain.length);
  144. }
  145.  
  146. function isIPAddress(domain) {
  147.     var result = ipPattern.exec(domain);
  148.     
  149.     if (result == null) {
  150.         return false;
  151.     }
  152.     
  153.     // testing for equality against result[0] ensures that
  154.     // we don't match strings like "123.123.45.6.foo.com"
  155.     return (domain == result[0]);
  156. }
  157.  
  158. function reloadOptions() {
  159.     loadShowYouAreOn();
  160.     loadShowOptionsButton();
  161.     loadDisplaySize();
  162.     loadDisplayColor();
  163.     loadShowFullHostName();
  164. }
  165.  
  166. function loadShowYouAreOn() {
  167.     try {
  168.         showYouAreOn = prefsBranch.getBoolPref(showYouAreOnPref);
  169.     } catch (e) {
  170.         prefsBranch.setBoolPref(showYouAreOnPref, true);
  171.         showYouAreOn = prefsBranch.getBoolPref(showYouAreOnPref);
  172.     }
  173. }
  174.  
  175. function loadShowOptionsButton() {
  176.     try {
  177.         showOptionsButton = prefsBranch.getBoolPref(showOptionsButtonPref);
  178.     } catch (e) {
  179.         prefsBranch.setBoolPref(showOptionsButtonPref, true);
  180.         showOptionsButton = prefsBranch.getBoolPref(showOptionsButtonPref);
  181.     }
  182. }
  183.  
  184. function loadDisplaySize() {
  185.     try {
  186.         displaySize = prefsBranch.getIntPref(displaySizePref);
  187.         
  188.         // ensure legal displaySize value
  189.         if (displaySize < 1) {
  190.             displaySize = 1;
  191.         } else if (displaySize > 3) {
  192.             displaySize = 3;
  193.         }
  194.     } catch (e) {
  195.         prefsBranch.setIntPref(displaySizePref, 2);
  196.         displaySize = prefsBranch.getIntPref(displaySizePref);
  197.     }
  198. }
  199.  
  200. function loadDisplayColor() {
  201.     try {
  202.         displayColor = prefsBranch.getCharPref(displayColorPref);
  203.     } catch (e) {
  204.         prefsBranch.setCharPref(displayColorPref, "#00BB00");
  205.         displayColor = prefsBranch.getCharPref(displayColorPref);
  206.     }
  207. }
  208.  
  209. function loadShowFullHostName() {
  210.     try {
  211.         showFullHostName = prefsBranch.getBoolPref(showFullHostNamePref);
  212.     } catch (e) {
  213.         prefsBranch.setBoolPref(showFullHostNamePref, false);
  214.         showFullHostName = prefsBranch.getBoolPref(showFullHostNamePref);
  215.     }
  216. }
  217.  
  218. function isCommonTLD(tld) {
  219.     for (var i = 0; i < commonTLDs.length; i++) {
  220.         if (tld == commonTLDs[i]) {            
  221.             return true;
  222.         }
  223.     }
  224.     
  225.     return false;
  226. }
  227.  
  228. function openOptionsWindow() {
  229.     window.open(optionsWindowURI, optionsWindowTitle, optionsWindowParams);
  230. }
  231.  
  232. function openAboutWindow() {
  233.     window.open(aboutWindowURI, aboutWindowTitle, aboutWindowParams);
  234. }
  235.  
  236. ////////////////////////////////////////
  237.  
  238. window.addEventListener("load", updateSpoofStick, true);
  239. window.addEventListener("unload", clearSpoofStick, true);
  240.  
  241. // make sure we update when the user selects another tab
  242. window.addEventListener("focus", updateSpoofStick, true);
  243.  
  244. reloadOptions();