home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 March / PCWorld_2005-03_cd.bin / komunikace / kmeleon / kmeleon09.exe / flashblock.jar / content / flashblock / flashblock.js < prev    next >
Text File  |  2004-12-07  |  7KB  |  243 lines

  1. /// USER STYLESHEET FUNCTIONS
  2.  
  3. // File mode flags
  4.  
  5. const MODE_RDONLY   = 0x01;
  6. const MODE_WRONLY   = 0x02;
  7. const MODE_RDWR     = 0x04;
  8. const MODE_CREATE   = 0x08;
  9. const MODE_APPEND   = 0x10;
  10. const MODE_TRUNCATE = 0x20;
  11. const MODE_SYNC     = 0x40;
  12. const MODE_EXCL     = 0x80;
  13.  
  14. // Returns a nsIFile for the specified file in the profile chrome directory
  15. function getUserChromeFile(fileName)
  16. {
  17.     var NSIFILE = Components.interfaces.nsIFile;
  18.     var dirLocator = Components.classes["@mozilla.org/file/directory_service;1"]
  19.                                .getService(Components.interfaces.nsIProperties);
  20.     var userChromePath = dirLocator.get("UChrm", NSIFILE).path;
  21.  
  22.     var file = Components.classes["@mozilla.org/file/local;1"]
  23.                          .createInstance(Components.interfaces.nsILocalFile);
  24.  
  25.     file.initWithPath(userChromePath);
  26.     file.append(fileName);
  27.  
  28.     return file;
  29. }
  30.  
  31. // Returns the contents of the specified file in the profile chrome directory
  32. function readUserChromeFile(fileName) {
  33.     var fileContents = "";
  34.     var file = getUserChromeFile(fileName);
  35.  
  36.     if(file.exists()) {
  37.         var ioFlags = MODE_RDONLY;
  38.  
  39.         // Get an input stream
  40.         var is = Components.classes["@mozilla.org/network/file-input-stream;1"]
  41.                            .createInstance( Components.interfaces.nsIFileInputStream);
  42.         is.init(file, ioFlags, 0, is.CLOSE_ON_EOF);
  43.         var sis = Components.classes["@mozilla.org/scriptableinputstream;1"]
  44.                             .createInstance( Components.interfaces.nsIScriptableInputStream);
  45.         sis.init(is);
  46.  
  47.         // Read the file in
  48.         while(sis.available() > 0)
  49.             fileContents += sis.read(sis.available());
  50.  
  51.         // Close streams
  52.         is.close();
  53.         sis.close();
  54.     }
  55.  
  56.     return fileContents;
  57. }
  58.  
  59. // Writes the specified contents into the specified file in the profile chrome directory
  60. function writeUserChromeFile(fileName, fileContents) {
  61.     var file = getUserChromeFile(fileName);
  62.     var ioFlags = MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE;
  63.     var perm = 0644;
  64.  
  65.     var os = Components.classes["@mozilla.org/network/file-output-stream;1"]
  66.                        .createInstance( Components.interfaces.nsIFileOutputStream);
  67.     os.init(file, ioFlags, perm, 0);
  68.  
  69.     var result = os.write(fileContents, fileContents.length);
  70.     os.close();
  71.  
  72.     return result;
  73. }
  74.  
  75. // Checks the if the user stylesheet already contains the import statement
  76. function userStyleSheetHasImport() {
  77.     var fileContents = readUserChromeFile('userContent.css');
  78.     var re = new RegExp("^[ \t]*@import.*chrome://flashblock/content/flashblock.css", "m");
  79.  
  80.     return re.test(fileContents);
  81. }
  82.  
  83. // Adds a CSS import statement for the flashblock stylesheet
  84. function addImportToUserStylesheet(fileName) {
  85.     var importStr = "@import url(chrome://flashblock/content/flashblock.css);"
  86.  
  87.     var fileContents = readUserChromeFile('userContent.css');
  88.     var re = new RegExp("^[ \t]*@import.*chrome://flashblock/content/flashblock.css", "m");
  89.  
  90.     if(re.test(fileContents))
  91.         return true;
  92.  
  93.     fileContents = importStr + "\n" + fileContents;
  94.  
  95.     var ret = writeUserChromeFile(fileName, fileContents);
  96.     return (ret == fileContents.length);
  97. }
  98.  
  99. // Removes the CSS import statement for the flashblock stylesheet
  100. function removeImportFromUserStylesheet(fileName) {
  101.     var fileContents = readUserChromeFile(fileName);
  102.     var re = new RegExp("^[ \t]*@import.*chrome://flashblock/content/flashblock.css.*(\n)?$", "mg");
  103.  
  104.     if(re.test(fileContents)) {
  105.         fileContents = fileContents.replace(re, '');
  106.         var ret = writeUserChromeFile(fileName, fileContents);
  107.         return (ret == fileContents.length)
  108.     } else {
  109.         return true;
  110.     }
  111. }
  112.  
  113.  
  114. /// PREF FUNCTIONS
  115. var prefObserver = {
  116.     observe: function(subject, topic, data) {
  117.         if(data == "flashblock.whitelist")
  118.             loadWhitelist();
  119.         if(data == "flashblock.enabled");
  120.             gEnabled = isEnabled();
  121.     }
  122. }
  123.  
  124. function addPrefObserver() {
  125.     var prefs = Components.classes["@mozilla.org/preferences-service;1"]
  126.                           .getService(Components.interfaces.nsIPrefBranch);
  127.  
  128.     var pbi = prefs.QueryInterface(Components.interfaces.nsIPrefBranchInternal);
  129.     pbi.addObserver("flashblock.enabled", prefObserver, false);
  130.     pbi.addObserver("flashblock.whitelist", prefObserver, false);
  131. }
  132.  
  133. // Loads the whitelist into the global array
  134. function loadWhitelist() {
  135.     gWhitelist = new Array();
  136.     pref = getWhitelistPref();
  137.     if (pref) {
  138.         gWhitelist = pref.split(",");
  139.         gWhitelist[gWhitelist.length] = "file://";
  140.     }
  141. }
  142.  
  143.  
  144. /// WHITELIST FUNCTIONS
  145.  
  146. function checkWhitelist(url) {
  147.     if(url.protocol == "file:")
  148.         return true;
  149.  
  150.     for (var i = 0; i < gWhitelist.length; i++) {
  151.         // Handle *
  152.         var expr = gWhitelist[i];
  153.         expr = expr.replace(/\./g, "\\.");
  154.         expr = expr.replace(/\-/g, "\\-");
  155.         expr = expr.replace(/\*/g, "[A-Za-z0-9_\\-\\.]+")
  156.         expr = "^" + expr + "$";
  157.  
  158.         var re = new RegExp(expr);
  159.         if(re.test(url.host))
  160.             return true;
  161.     }
  162.     return false;
  163. }
  164.  
  165. function checkLoadFlash(e) {
  166.     if(! gEnabled || checkWhitelist(e.target.location))
  167.         e.preventDefault();
  168.     e.stopPropagation();
  169. }
  170.  
  171.  
  172. /// CONTEXT MENU FUNCTIONS
  173.  
  174. function contextMenuInit() {
  175.     var cm = document.getElementById("contentAreaContextMenu");
  176.     cm.addEventListener("popupshowing",flashblockContextMenu,false);
  177. }
  178.  
  179. function flashblockContextMenu() {
  180.      var cm = gContextMenu;
  181.     var onFlash = cm.onImage && cm.imageURL.indexOf("chrome://flashblock") == 0;
  182.  
  183.     document.getElementById("context-flashAllow").hidden = !onFlash;
  184.     document.getElementById("context-flashWhitelist").hidden = !onFlash;
  185.  
  186.     if(onFlash) {
  187.         // XXX HACK: isn't there a better way?
  188.         document.getElementById("context-saveimage").hidden = true;
  189.         document.getElementById("context-sendimage").hidden = true;
  190.         document.getElementById("context-setWallpaper").hidden = true;
  191.         document.getElementById("context-viewimage").hidden = true;
  192.         document.getElementById("context-blockimage").hidden = true;
  193.         document.getElementById("context-copyimage").hidden = true;
  194.         document.getElementById("context-sep-copyimage").hidden = true;
  195.         document.getElementById("context-sep-properties").hidden = true;
  196.         document.getElementById("context-metadata").hidden = true;
  197.         try {
  198.             document.getElementById("context-copyimage-contents").hidden = true;
  199.         } catch(e) {}
  200.     }
  201. }
  202.  
  203. function flashblockOptions() {
  204.     window.openDialog("chrome://flashblock/content/options.xul", "FlashblockOptions", "chrome");
  205. }
  206.  
  207. function addSiteToWhitelist() {
  208.     var uri = Components.classes['@mozilla.org/network/standard-url;1']
  209.         .createInstance(Components.interfaces.nsIURI);
  210.     uri.spec = gContextMenu.target.baseURI;
  211.  
  212.     var prefStr = getWhitelistPref();
  213.     if(prefStr)
  214.         prefStr += ","
  215.  
  216.     if(prefStr.indexOf(uri.host + ",") == -1)
  217.         prefStr += uri.host;
  218.         setWhitelistPref(prefStr);
  219. }
  220.  
  221.  
  222. /// INITIALIZATION CODE
  223.  
  224. function onInstall() {
  225.     window.removeEventListener("load", onInstall, true);
  226.  
  227.     if(! userStyleSheetHasImport()) {
  228.         addImportToUserStylesheet('userContent.css')
  229.         alert('Flashblock is now installed.\nPlease restart firefox to activate it.');
  230.     }
  231. }
  232.  
  233. var gWhitelist;
  234. var gEnabled;
  235.  
  236. addPrefObserver();
  237. loadWhitelist();
  238. gEnabled = isEnabled();
  239.  
  240. window.addEventListener("load", onInstall, true);
  241. window.addEventListener("flashblockCheckLoad", checkLoadFlash, false)
  242. window.addEventListener("load", contextMenuInit, false);
  243.