home *** CD-ROM | disk | FTP | other *** search
- /*
- * SpoofStick - Main Extension Code
- *
- * Copyright (C) 2004 CoreStreet, Ltd.
- * http://www.corestreet.com/
- *
- * Author: Mark Ayzenshtat
- */
-
- const targetID = "spoofstick-desc";
- const youAreOnID = "spoofstick-youareon";
- const optionsButtonID = "spoofstick-options";
- const labelAttr = "value";
- const notVisibleAttr = "collapsed";
-
- const prefsClassString = "@mozilla.org/preferences-service;1";
- const branchName = "spoofstick.";
- const showYouAreOnPref = "showYouAreOn";
- const showOptionsButtonPref = "showOptionsButton";
- const displaySizePref = "size";
- const size1Pref = "size1";
- const size2Pref = "size2";
- const size3Pref = "size3";
- const displayColorPref = "color";
- const showFullHostNamePref = "showFullHostName";
-
- const optionsWindowURI = "chrome://spoofstick/content/options.xul";
- const optionsWindowTitle = "SpoofStick Options";
- const optionsWindowParams = "chrome,centerscreen";
-
- const aboutWindowURI = "chrome://spoofstick/content/about.xul";
- const aboutWindowTitle = "About SpoofStick";
- const aboutWindowParams = "chrome,centerscreen";
-
- // basic regular expression for IP addresses
- const ipPattern = /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/;
-
- const fileProtocolText = "(local filesystem)";
-
- var commonTLDs = ["com", "org", "net", "edu", "gov", "mil"];
-
- var showYouAreOn;
- var showOptionsButton;
- var displaySize;
- var displayColor;
- var showFullHostName;
-
- // preferences-related objects
- var prefs = Components.classes[prefsClassString].
- getService(Components.interfaces.nsIPrefService);
- var prefsBranch = prefs.getBranch(branchName);
-
- function updateSpoofStick(eventOrPage) {
- var page = getPage(eventOrPage);
-
- // reload options every time we update so that
- // users don't have to restart their browsers
- reloadOptions();
-
- updateControls();
-
- var ssTarget = document.getElementById(targetID);
- ssTarget.setAttribute(labelAttr, getDomainName(page));
- }
-
- function clearSpoofStick() {
- var ssTarget = document.getElementById(targetID);
- ssTarget.setAttribute(labelAttr, "");
- }
-
- function updateControls() {
- updateShowYouAreOn();
- updateShowOptionsButton();
- updateDisplay();
- }
-
- function updateShowYouAreOn() {
- document.getElementById(youAreOnID).setAttribute(notVisibleAttr, !showYouAreOn);
- }
-
- function updateShowOptionsButton() {
- document.getElementById(optionsButtonID).setAttribute(notVisibleAttr, !showOptionsButton);
- }
-
- function updateDisplay() {
- var ssTarget = document.getElementById(targetID);
- var youAreOn = document.getElementById(youAreOnID);
-
- // update color
- ssTarget.setAttribute("style", "color:" + displayColor + ";");
-
- // update size
- ssTarget.setAttribute("class", "size" + displaySize);
- youAreOn.setAttribute("class", "size" + displaySize);
- }
-
- function getPage(eventOrPage) {
- if (eventOrPage.type) {
- return eventOrPage.target.contentDocument;
- } else {
- return eventOrPage;
- }
- }
-
- function getDomainName(p) {
- if (p.URL.indexOf("file:") == 0) {
- // user is browsing their local filesystem
- return fileProtocolText;
- }
-
- if (showFullHostName) {
- return p.location.hostname;
- }
-
- // otherwise, show just the domain name
-
- var rawDomain = p.domain;
-
- var firstDot = rawDomain.indexOf(".");
- var lastDot = rawDomain.lastIndexOf(".");
-
- if (firstDot == lastDot) {
- return rawDomain;
- }
-
- var dot2FromEnd = rawDomain.lastIndexOf(".", lastDot - 1);
- var dot3FromEnd = rawDomain.lastIndexOf(".", dot2FromEnd - 1);
-
- // if the domain has a common TLD like com, net, org, etc.
- // or if it only has 3 tokens, return the last 2
- var tld = rawDomain.substring(lastDot + 1, rawDomain.length);
- if (dot3FromEnd == -1 || isCommonTLD(tld)) {
- return rawDomain.substring(dot2FromEnd + 1, rawDomain.length);
- }
-
- // if the domain name is an IP address, return it whole
- if (isIPAddress(rawDomain)) {
- return rawDomain;
- }
-
- // assume some other TLD with at least 4 tokens -
- // return the last 3
- return rawDomain.substring(dot3FromEnd + 1, rawDomain.length);
- }
-
- function isIPAddress(domain) {
- var result = ipPattern.exec(domain);
-
- if (result == null) {
- return false;
- }
-
- // testing for equality against result[0] ensures that
- // we don't match strings like "123.123.45.6.foo.com"
- return (domain == result[0]);
- }
-
- function reloadOptions() {
- loadShowYouAreOn();
- loadShowOptionsButton();
- loadDisplaySize();
- loadDisplayColor();
- loadShowFullHostName();
- }
-
- function loadShowYouAreOn() {
- try {
- showYouAreOn = prefsBranch.getBoolPref(showYouAreOnPref);
- } catch (e) {
- prefsBranch.setBoolPref(showYouAreOnPref, true);
- showYouAreOn = prefsBranch.getBoolPref(showYouAreOnPref);
- }
- }
-
- function loadShowOptionsButton() {
- try {
- showOptionsButton = prefsBranch.getBoolPref(showOptionsButtonPref);
- } catch (e) {
- prefsBranch.setBoolPref(showOptionsButtonPref, true);
- showOptionsButton = prefsBranch.getBoolPref(showOptionsButtonPref);
- }
- }
-
- function loadDisplaySize() {
- try {
- displaySize = prefsBranch.getIntPref(displaySizePref);
-
- // ensure legal displaySize value
- if (displaySize < 1) {
- displaySize = 1;
- } else if (displaySize > 3) {
- displaySize = 3;
- }
- } catch (e) {
- prefsBranch.setIntPref(displaySizePref, 2);
- displaySize = prefsBranch.getIntPref(displaySizePref);
- }
- }
-
- function loadDisplayColor() {
- try {
- displayColor = prefsBranch.getCharPref(displayColorPref);
- } catch (e) {
- prefsBranch.setCharPref(displayColorPref, "#00BB00");
- displayColor = prefsBranch.getCharPref(displayColorPref);
- }
- }
-
- function loadShowFullHostName() {
- try {
- showFullHostName = prefsBranch.getBoolPref(showFullHostNamePref);
- } catch (e) {
- prefsBranch.setBoolPref(showFullHostNamePref, false);
- showFullHostName = prefsBranch.getBoolPref(showFullHostNamePref);
- }
- }
-
- function isCommonTLD(tld) {
- for (var i = 0; i < commonTLDs.length; i++) {
- if (tld == commonTLDs[i]) {
- return true;
- }
- }
-
- return false;
- }
-
- function openOptionsWindow() {
- window.open(optionsWindowURI, optionsWindowTitle, optionsWindowParams);
- }
-
- function openAboutWindow() {
- window.open(aboutWindowURI, aboutWindowTitle, aboutWindowParams);
- }
-
- ////////////////////////////////////////
-
- window.addEventListener("load", updateSpoofStick, true);
- window.addEventListener("unload", clearSpoofStick, true);
-
- // make sure we update when the user selects another tab
- window.addEventListener("focus", updateSpoofStick, true);
-
- reloadOptions();