exposedProps

<<

Stefanvd

Posts: 39

Joined: Thu Sep 30, 2010 2:00 pm

Post Fri Aug 31, 2012 1:36 am

exposedProps

Hi,

I want to update my "Turn Off the Lights" addon code. Because i receive this error message:
"
Error: Exposing chrome JS objects to content without __exposedProps__ is insecure and deprecated. See https://developer.mozilla.org/en/XPConnect_wrappers for more information.
Source File: chrome://turnoffthelights/content/main.js
Line: 236
"
So i add this code in my main.js:
  Code:
sandbox.__exposedProps__ = { safeWin : "wr"};


But it still give me that error message. Any idea how to fix this?

Thanks,
Stefan
<<

jorge-villalobos

AMO Administrator

Posts: 2990

Joined: Tue Sep 29, 2009 7:30 pm

Location: San José, Costa Rica

Post Tue Sep 04, 2012 10:09 am

Re: exposedProps

Did you read this blog post? If that doesn't help, you should post more details, like which object member you want to share with the page and how it is being accessed from the page.
<<

Stefanvd

Posts: 39

Joined: Thu Sep 30, 2010 2:00 pm

Post Tue Sep 04, 2012 10:20 am

Re: exposedProps

Yes i read that blog post. That's the reason i try to update my addon.

And got no idea, what can be wrong in the above code :/
Here the full code in my main.js
I receive the error (firefox15) on this code line: "sandbox.window = safeWin;"

  Code:
// main script
// TurnOfftheLights namespace.
if ("undefined" == typeof(TurnOfftheLights)) {
  var TurnOfftheLights = {};
};

(function(ns) {
    ns.extension_id = "Turn Off the Lights (Firefox) extension";
   var prefManager = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
   
    ns.init = function () {

// welcome page
var firstrun = Services.prefs.getBoolPref("extensions.TurnOfftheLights.firstrun");
var curVersion = "2.1.0.3";

if (firstrun) {
// Adding button by default ------------
  var myId    = "TurnOfftheLights-button"; // ID of button to add
  var afterId = "urlbar-container";    // ID of element to insert after
  var navBar  = document.getElementById("nav-bar");
  var currentSet = navBar.getAttribute("currentset");
    if (!currentSet)
        currentSet = navBar.currentSet;

    var curSet = currentSet.split(",");
    if (curSet.indexOf(myId) == -1){
      var set = curSet.concat(myId);
      navBar.setAttribute("currentset", set.join(","));
      navBar.currentSet = set.join(",");
      document.persist(navBar.id, "currentset");
    try {
      BrowserToolboxCustomizeDone(true);
    }
    catch (e) {}
  }
//------------
  Services.prefs.setBoolPref("extensions.TurnOfftheLights.firstrun", false);
  Services.prefs.setCharPref("extensions.TurnOfftheLights.installedVersion", curVersion);
  /* Code related to firstrun */
  window.setTimeout(function(){ gBrowser.selectedTab = gBrowser.addTab("http://www.stefanvd.net/project/totlfirefoxguide.htm"); }, 1000);
  window.setTimeout(function(){ gBrowser.selectedTab = gBrowser.addTab("http://www.stefanvd.net/project/totlfirefox.htm"); }, 1000);
} else {
  try {
    var installedVersion = Services.prefs.getCharPref("extensions.TurnOfftheLights.installedVersion");
    if (curVersion > installedVersion) {
      Services.prefs.setCharPref("extensions.TurnOfftheLights.installedVersion", curVersion);
      /* Code related to upgrade */
     window.setTimeout(function(){ gBrowser.selectedTab = gBrowser.addTab("http://www.stefanvd.net/project/totlfirefoxupgrade.htm"); }, 1000);    
    }
  } catch (ex) {
    /* Code related to a reinstall */
  }
}   

   // For accessing browser window from sidebar code.
    var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
               .getInterface(Components.interfaces.nsIWebNavigation)
               .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
               .rootTreeItem
               .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
               .getInterface(Components.interfaces.nsIDOMWindow);
    var gBrowser = mainWindow.gBrowser;
    if (gBrowser.addEventListener) {
        gBrowser.addEventListener("DOMContentLoaded",pageLoaded,true);
    }
      
   function pageLoaded(aEvent) {
    if ((aEvent.originalTarget.nodeName == '#document') && (aEvent.originalTarget.defaultView.location.href == gBrowser.currentURI.spec))
    {
   var doc = aEvent.originalTarget; // loaded document
    var win = doc.defaultView; // loaded window
    if (win.frameElement) return; // ignore frames
   
   var tab = documentToTab(gBrowser.contentDocument);
   
   // check if not on firefox "about" or "chrome" page
   var checktaburl = window.top.getBrowser().selectedBrowser.contentWindow.location.protocol;
   if(checktaburl != "chrome:"){
   firefox.tabs.executeScript(tab, {file: "chrome://TurnOfftheLights/content/js/injected.js"});
   firefox.tabs.executeScript(tab, {file: "chrome://TurnOfftheLights/content/js/content.js"});
   }
   
         var contextmenus = prefManager.getBoolPref("extensions.TurnOfftheLights.contextmenus");
         if (contextmenus) {
         actionmenu();
         } else {
         document.getElementById("TurnOfftheLightspage").style.display = "none";
         document.getElementById("TurnOfftheLightsvideo").style.display = "none";
         }
    } // end close content script
}
            
function actionmenu() {
   // context menu
   var AreaContextMenu = document.getElementById("contentAreaContextMenu");
   if (AreaContextMenu) {AreaContextMenu.addEventListener("popupshowing", ShowHideItems, false);}
               
      function ShowHideItems(event)
      {
         if (document.popupNode.localName.toUpperCase() == "VIDEO") {
            document.getElementById("TurnOfftheLightspage").style.display = "none";
            document.getElementById("TurnOfftheLightsvideo").style.display = "block";
         }
         else if (document.popupNode.localName.toUpperCase() != "VIDEO") {
            document.getElementById("TurnOfftheLightspage").style.display = "block";
            document.getElementById("TurnOfftheLightsvideo").style.display = "none";
         }
      }
}

        firefox.extension.onRequest.addListener(function(request, sender, sendResponse) {
            if ( request.name == "totlrequest" ) {
            sendResponse({interval: prefManager.getCharPref("extensions.TurnOfftheLights.interval"), lightcolor: prefManager.getCharPref("extensions.TurnOfftheLights.lightcolor"), autoplay: prefManager.getBoolPref("extensions.TurnOfftheLights.autoplay"), playlist: prefManager.getBoolPref("extensions.TurnOfftheLights.playlist"), flash: prefManager.getBoolPref("extensions.TurnOfftheLights.flash"), head: prefManager.getBoolPref("extensions.TurnOfftheLights.head"), fadein: prefManager.getBoolPref("extensions.TurnOfftheLights.fadein"), fadeout: prefManager.getBoolPref("extensions.TurnOfftheLights.fadeout"), infobar: prefManager.getBoolPref("extensions.TurnOfftheLights.infobar"), sharebutton: prefManager.getBoolPref("extensions.TurnOfftheLights.sharebutton"), likebutton: prefManager.getBoolPref("extensions.TurnOfftheLights.likebutton"), readera: prefManager.getBoolPref("extensions.TurnOfftheLights.readera"), readern: prefManager.getBoolPref("extensions.TurnOfftheLights.readern"), shortcutlight: prefManager.getBoolPref("extensions.TurnOfftheLights.shortcutlight"), eyea: prefManager.getBoolPref("extensions.TurnOfftheLights.eyea"), eyen: prefManager.getBoolPref("extensions.TurnOfftheLights.eyen"), suggestions: prefManager.getBoolPref("extensions.TurnOfftheLights.suggestions"), videoheadline: prefManager.getBoolPref("extensions.TurnOfftheLights.videoheadline"), eastereggs: prefManager.getBoolPref("extensions.TurnOfftheLights.eastereggs"), contextmenus: prefManager.getBoolPref("extensions.TurnOfftheLights.contextmenus"), readerlargestyle: prefManager.getBoolPref("extensions.TurnOfftheLights.readerlargestyle"), viewcount: prefManager.getBoolPref("extensions.TurnOfftheLights.viewcount"), lightimage: prefManager.getCharPref("extensions.TurnOfftheLights.lightimage"), lightimagea: prefManager.getBoolPref("extensions.TurnOfftheLights.lightimagea"), lightimagen: prefManager.getBoolPref("extensions.TurnOfftheLights.lightimagen"), eyealist: prefManager.getBoolPref("extensions.TurnOfftheLights.eyealist"), excludedDomains: prefManager.getCharPref("extensions.TurnOfftheLights.excludedDomains"), mousespotlighto: prefManager.getBoolPref("extensions.TurnOfftheLights.mousespotlighto"), mousespotlighta: prefManager.getBoolPref("extensions.TurnOfftheLights.mousespotlighta"), mousespotlightc: prefManager.getBoolPref("extensions.TurnOfftheLights.mousespotlightc"), videodefault: prefManager.getBoolPref("extensions.TurnOfftheLights.videodefault"), videocloud: prefManager.getBoolPref("extensions.TurnOfftheLights.videocloud"), videocloudfile: prefManager.getCharPref("extensions.TurnOfftheLights.videocloudfile"), expirationdate: prefManager.getCharPref("extensions.TurnOfftheLights.expirationdate"), nighttime: prefManager.getBoolPref("extensions.TurnOfftheLights.nighttime"), begintime: prefManager.getCharPref("extensions.TurnOfftheLights.begintime"), endtime: prefManager.getCharPref("extensions.TurnOfftheLights.endtime"), cloudfileversion: prefManager.getCharPref("extensions.TurnOfftheLights.cloudfileversion"), addvideobutton: prefManager.getBoolPref("extensions.TurnOfftheLights.addvideobutton"), likebar: prefManager.getBoolPref("extensions.TurnOfftheLights.likebar"), ambilight: prefManager.getBoolPref("extensions.TurnOfftheLights.ambilight"), ambilightrangeblurradius: prefManager.getCharPref("extensions.TurnOfftheLights.ambilightrangeblurradius"), ambilightrangespreadradius: prefManager.getCharPref("extensions.TurnOfftheLights.ambilightrangespreadradius"), mousespotlightt: prefManager.getBoolPref("extensions.TurnOfftheLights.mousespotlightt"), ambilightfixcolor: prefManager.getBoolPref("extensions.TurnOfftheLights.ambilightfixcolor"), ambilightvarcolor: prefManager.getBoolPref("extensions.TurnOfftheLights.ambilightvarcolor"), ambilightcolorhex: prefManager.getCharPref("extensions.TurnOfftheLights.ambilightcolorhex"), ambilight4color: prefManager.getBoolPref("extensions.TurnOfftheLights.ambilight4color"), ambilight1colorhex: prefManager.getCharPref("extensions.TurnOfftheLights.ambilight1colorhex"), ambilight2colorhex: prefManager.getCharPref("extensions.TurnOfftheLights.ambilight2colorhex"), ambilight3colorhex: prefManager.getCharPref("extensions.TurnOfftheLights.ambilight3colorhex"), ambilight4colorhex: prefManager.getCharPref("extensions.TurnOfftheLights.ambilight4colorhex"), password: prefManager.getBoolPref("extensions.TurnOfftheLights.password"), enterpassword: prefManager.getCharPref("extensions.TurnOfftheLights.enterpassword"), noflash: prefManager.getBoolPref("extensions.TurnOfftheLights.noflash"), hardflash: prefManager.getBoolPref("extensions.TurnOfftheLights.hardflash"), ecosaver: prefManager.getBoolPref("extensions.TurnOfftheLights.ecosaver"), ecosavertime: prefManager.getCharPref("extensions.TurnOfftheLights.ecosavertime"), dynamic: prefManager.getBoolPref("extensions.TurnOfftheLights.dynamic"), dynamic1: prefManager.getBoolPref("extensions.TurnOfftheLights.dynamic1"), dynamic2: prefManager.getBoolPref("extensions.TurnOfftheLights.dynamic2"), dynamic3: prefManager.getBoolPref("extensions.TurnOfftheLights.dynamic3"), dynamic4: prefManager.getBoolPref("extensions.TurnOfftheLights.dynamic4"), dynamic5: prefManager.getBoolPref("extensions.TurnOfftheLights.dynamic5")});
         }
         else if ( request.name == "automatic" ) {
            var tab = documentToTab(gBrowser.contentDocument);
            firefox.tabs.executeScript(sender.tab, {file: "chrome://TurnOfftheLights/content/js/light.js"});
         }
         else if ( request.name == "readersaveme" ) {
            Services.prefs.setCharPref("extensions.TurnOfftheLights.interval", request.value);
         }
         else if ( request.name == "readerlargestyle" ) {
            Services.prefs.setBoolPref("extensions.TurnOfftheLights.readerlargestyle", request.value);
         }
        });
    };
   
    ns.onToolbarButtonClick = function() {
        var tab = documentToTab(gBrowser.contentDocument);
        firefox.tabs.executeScript(tab, {file: "chrome://TurnOfftheLights/content/js/light.js"});
    };
   
    ns.shortcut = function() {
      var shortcutlight = prefManager.getBoolPref("extensions.TurnOfftheLights.shortcutlight");
      if (shortcutlight) {
         var tab = documentToTab(gBrowser.contentDocument);
         firefox.tabs.executeScript(tab, {file: "chrome://TurnOfftheLights/content/js/light.js"});
      }
    };
   
    ns.keytheater = function() {
        var tab = documentToTab(gBrowser.contentDocument);
      firefox.tabs.sendRequest(tab, {name: "theater"});
    };
   
    var documentToTab = function(doc) {
        return {
            document: doc,
            url: doc.location.href
        }
    };

    var evalInSandbox = function(code, codebase, sandbox ){
        if (Components.utils && Components.utils.Sandbox) {
            // DP beta+
            Components.utils.evalInSandbox(code, sandbox);
        } else if (Components.utils && Components.utils.evalInSandbox) {
            // DP alphas
            Components.utils.evalInSandbox(code, codebase, sandbox);
        } else if (Sandbox) {
            // 1.0.x
            evalInSandbox(code, sandbox, codebase);
        } else {
            throw new Error("Could not create sandbox.");
        }
    };

    var getFileContent = function( file ) {
        var ioService=Components.classes["@mozilla.org/network/io-service;1"]
            .getService(Components.interfaces.nsIIOService);
        var scriptableStream=Components
            .classes["@mozilla.org/scriptableinputstream;1"]
            .getService(Components.interfaces.nsIScriptableInputStream);
        var unicodeConverter=Components
            .classes["@mozilla.org/intl/scriptableunicodeconverter"]
            .createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
        unicodeConverter.charset="UTF-8";

        var channel=ioService.newChannel( file, "UTF-8", null);
        var input=channel.open();
        scriptableStream.init(input);
        var str=scriptableStream.read(input.available());
        scriptableStream.close();
        input.close();

        try {
            return unicodeConverter.ConvertToUnicode(str);
        } catch (e) {
            return str;
        }
    };

    var console = {};
    console._service = Components.classes["@mozilla.org/consoleservice;1"]
        .getService(Components.interfaces.nsIConsoleService);
    console.log = function(msg) {
        console._service.logStringMessage(msg);
    };

    var firefox = {};

    firefox.tabs = {};
    firefox.tabs.executeScript = function(tab, details) {
        var unsafeWin = tab.document.defaultView;
        var safeWin = new XPCNativeWrapper(unsafeWin);
        var sandbox = new Components.utils.Sandbox(safeWin);
        sandbox.window = safeWin;
        sandbox.document = sandbox.window.document;
        sandbox.unsafeWindow = unsafeWin;
        sandbox.__proto__ = sandbox.window;
        sandbox.firefox = {};
        sandbox.firefox.extension = new firefox.tabs.extension(tab.document);
        sandbox.console = console;
        var code = (typeof details.code != "undefined"
                    ? details.code
                    : ( typeof details.file != "undefined" ? getFileContent(details.file) : undefined ) );
        if ( code ) {
            try {
                evalInSandbox( code, tab.url, sandbox );
            } catch ( e ) {
                console.log("[ERROR] " + e );
            }
        }
    };

    firefox.tabs.event_id = ns.extension_id+".tabs.Request";
    firefox.tabs.request = {};
   
    firefox.tabs.sendRequest = function(tab, request, requestCallback) {
        var evt = tab.document.createEvent("Events");
        evt.initEvent(firefox.tabs.event_id, true, false);
        firefox.tabs.request = {
            data: request,
            callback: requestCallback
        };
        tab.document.dispatchEvent(evt);
    };

    firefox.tabs.extension = function(doc) {
        this.document = doc;
        this.event_id = firefox.tabs.event_id;

        var listeners = [];
        var handler = function(evt) {
            var request = firefox.tabs.request;
            for ( var i=0; i<listeners.length; i++ ) {
                listeners[i](request.data, null, request.callback);
            }
        };

        this.onRequest = {
            addListener: function(listener) {
                listeners.push(listener);
            },
           
            removeListener: function(listener) {
                for ( var i=0; i<listeners.length; i++ ) {
                    if ( listener == listeners[i] ) {
                        listeners.splice(i, 1);
                    }
                }
            }
        };
        doc.addEventListener(firefox.tabs.event_id, handler, false, true );
    };
   
    var listeners = [];
    firefox.tabs.extension.prototype.sendRequest = function(request, responseCallback) {
        if ( typeof responseCallback!="function" ) {
            responseCallback = function() {};
        }
        for ( var i=0; i<listeners.length; i++ ) {
            listeners[i](request, { tab: documentToTab(this.document) }, responseCallback);
        }
    };

    firefox.extension = {};
    firefox.extension.onRequest = {
        addListener: function(listener) {
            listeners.push(listener);
        },
        removeListener: function(listener) {
            for ( var i=0; i<listeners.length; i++ ) {
                if ( listener == listeners[i] ) {
                    listeners.splice(i, 1);
                }
            }
        }
    };
})(TurnOfftheLights);

window.addEventListener("load", function() {TurnOfftheLights.init();}, false);
<<

jorge-villalobos

AMO Administrator

Posts: 2990

Joined: Tue Sep 29, 2009 7:30 pm

Location: San José, Costa Rica

Post Tue Sep 04, 2012 10:57 am

Re: exposedProps

I don't quite understand what you're doing with the sandbox stuff, so I don't know if you're doing something wrong or running into a bug in the implementation of exposedProps. Since your objective is to effectively run a userscript, have you looked into what GreaseMonkey or Scriptish do? They must have solved this problem by now, so their source code is the best place to find your answer.
<<

Stefanvd

Posts: 39

Joined: Thu Sep 30, 2010 2:00 pm

Post Tue Sep 04, 2012 11:09 am

Re: exposedProps

The code i provide from main.js works. But it only show that error message in the consolelog from firefox 15.
<<

jorge-villalobos

AMO Administrator

Posts: 2990

Joined: Tue Sep 29, 2009 7:30 pm

Location: San José, Costa Rica

Post Tue Sep 04, 2012 1:27 pm

Re: exposedProps

It'll probably stop working on Firefox 17. I recommend that you test on Aurora and see if there are any problems.
<<

Stefanvd

Posts: 39

Joined: Thu Sep 30, 2010 2:00 pm

Post Fri Sep 07, 2012 1:29 pm

Re: exposedProps

Fixed now.
Strange i doesn't need to use the __exposedProps__ , only i must change the var sandbox. (and it work on firefox 16 and aurora)

See change on the begin from "firefox.tabs.executeScript = function(tab, details) {"

  Code:
// main script
// TurnOfftheLights namespace.
if ("undefined" == typeof(TurnOfftheLights)) {
  var TurnOfftheLights = {};
};

(function(ns) {
    ns.extension_id = "Turn Off the Lights (Firefox) extension";
   var prefManager = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
   
    ns.init = function () {

// welcome page
var firstrun = Services.prefs.getBoolPref("extensions.TurnOfftheLights.firstrun");
var curVersion = "2.1.0.7";

if (firstrun) {
// Adding button by default ------------
  var myId    = "TurnOfftheLights-button"; // ID of button to add
  var afterId = "urlbar-container";    // ID of element to insert after
  var navBar  = document.getElementById("nav-bar");
  var currentSet = navBar.getAttribute("currentset");
    if (!currentSet)
        currentSet = navBar.currentSet;

    var curSet = currentSet.split(",");
    if (curSet.indexOf(myId) == -1){
      var set = curSet.concat(myId);
      navBar.setAttribute("currentset", set.join(","));
      navBar.currentSet = set.join(",");
      document.persist(navBar.id, "currentset");
    try {
      BrowserToolboxCustomizeDone(true);
    }
    catch (e) {}
  }
//------------
  Services.prefs.setBoolPref("extensions.TurnOfftheLights.firstrun", false);
  Services.prefs.setCharPref("extensions.TurnOfftheLights.installedVersion", curVersion);
  /* Code related to firstrun */
  window.setTimeout(function(){ gBrowser.selectedTab = gBrowser.addTab("http://www.stefanvd.net/project/totlfirefoxguide.htm"); }, 1000);
  window.setTimeout(function(){ gBrowser.selectedTab = gBrowser.addTab("http://www.stefanvd.net/project/totlfirefox.htm"); }, 1000);
} else {
  try {
    var installedVersion = Services.prefs.getCharPref("extensions.TurnOfftheLights.installedVersion");
    if (curVersion > installedVersion) {
      Services.prefs.setCharPref("extensions.TurnOfftheLights.installedVersion", curVersion);
      /* Code related to upgrade */
     window.setTimeout(function(){ gBrowser.selectedTab = gBrowser.addTab("http://www.stefanvd.net/project/totlfirefoxupgrade.htm"); }, 1000);    
    }
  } catch (ex) {
    /* Code related to a reinstall */
  }
}   

   // For accessing browser window from sidebar code.
    var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
               .getInterface(Components.interfaces.nsIWebNavigation)
               .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
               .rootTreeItem
               .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
               .getInterface(Components.interfaces.nsIDOMWindow);
    var gBrowser = mainWindow.gBrowser;
    if (gBrowser.addEventListener) {
        gBrowser.addEventListener("DOMContentLoaded",pageLoaded,true);
    }
      
   function pageLoaded(aEvent) {
    if ((aEvent.originalTarget.nodeName == '#document') && (aEvent.originalTarget.defaultView.location.href == gBrowser.currentURI.spec))
    {
   var doc = aEvent.originalTarget; // loaded document
    var win = doc.defaultView; // loaded window
    if (win.frameElement) return; // ignore frames
   
   var tab = documentToTab(gBrowser.contentDocument);
   
   // check if not on firefox "about" or "chrome" page
   var checktaburl = window.top.getBrowser().selectedBrowser.contentWindow.location.protocol;
   if(checktaburl != "chrome:"){
   firefox.tabs.executeScript(tab, {file: "chrome://TurnOfftheLights/content/js/injected.js"});
   firefox.tabs.executeScript(tab, {file: "chrome://TurnOfftheLights/content/js/content.js"});
   }
   
         var contextmenus = prefManager.getBoolPref("extensions.TurnOfftheLights.contextmenus");
         if (contextmenus) {
         actionmenu();
         } else {
         document.getElementById("TurnOfftheLightspage").style.display = "none";
         document.getElementById("TurnOfftheLightsvideo").style.display = "none";
         }
    } // end close content script
}
            
function actionmenu() {
   // context menu
   var AreaContextMenu = document.getElementById("contentAreaContextMenu");
   if (AreaContextMenu) {AreaContextMenu.addEventListener("popupshowing", ShowHideItems, false);}
               
      function ShowHideItems(event)
      {
         if (document.popupNode.localName.toUpperCase() == "VIDEO") {
            document.getElementById("TurnOfftheLightspage").style.display = "none";
            document.getElementById("TurnOfftheLightsvideo").style.display = "block";
         }
         else if (document.popupNode.localName.toUpperCase() != "VIDEO") {
            document.getElementById("TurnOfftheLightspage").style.display = "block";
            document.getElementById("TurnOfftheLightsvideo").style.display = "none";
         }
      }
}

        firefox.extension.onRequest.addListener(function(request, sender, sendResponse) {
            if ( request.name == "totlrequest" ) {
            sendResponse({interval: prefManager.getCharPref("extensions.TurnOfftheLights.interval"), lightcolor: prefManager.getCharPref("extensions.TurnOfftheLights.lightcolor"), autoplay: prefManager.getBoolPref("extensions.TurnOfftheLights.autoplay"), playlist: prefManager.getBoolPref("extensions.TurnOfftheLights.playlist"), flash: prefManager.getBoolPref("extensions.TurnOfftheLights.flash"), head: prefManager.getBoolPref("extensions.TurnOfftheLights.head"), fadein: prefManager.getBoolPref("extensions.TurnOfftheLights.fadein"), fadeout: prefManager.getBoolPref("extensions.TurnOfftheLights.fadeout"), infobar: prefManager.getBoolPref("extensions.TurnOfftheLights.infobar"), sharebutton: prefManager.getBoolPref("extensions.TurnOfftheLights.sharebutton"), likebutton: prefManager.getBoolPref("extensions.TurnOfftheLights.likebutton"), readera: prefManager.getBoolPref("extensions.TurnOfftheLights.readera"), readern: prefManager.getBoolPref("extensions.TurnOfftheLights.readern"), shortcutlight: prefManager.getBoolPref("extensions.TurnOfftheLights.shortcutlight"), eyea: prefManager.getBoolPref("extensions.TurnOfftheLights.eyea"), eyen: prefManager.getBoolPref("extensions.TurnOfftheLights.eyen"), suggestions: prefManager.getBoolPref("extensions.TurnOfftheLights.suggestions"), videoheadline: prefManager.getBoolPref("extensions.TurnOfftheLights.videoheadline"), eastereggs: prefManager.getBoolPref("extensions.TurnOfftheLights.eastereggs"), contextmenus: prefManager.getBoolPref("extensions.TurnOfftheLights.contextmenus"), readerlargestyle: prefManager.getBoolPref("extensions.TurnOfftheLights.readerlargestyle"), viewcount: prefManager.getBoolPref("extensions.TurnOfftheLights.viewcount"), lightimage: prefManager.getCharPref("extensions.TurnOfftheLights.lightimage"), lightimagea: prefManager.getBoolPref("extensions.TurnOfftheLights.lightimagea"), lightimagen: prefManager.getBoolPref("extensions.TurnOfftheLights.lightimagen"), eyealist: prefManager.getBoolPref("extensions.TurnOfftheLights.eyealist"), excludedDomains: prefManager.getCharPref("extensions.TurnOfftheLights.excludedDomains"), mousespotlighto: prefManager.getBoolPref("extensions.TurnOfftheLights.mousespotlighto"), mousespotlighta: prefManager.getBoolPref("extensions.TurnOfftheLights.mousespotlighta"), mousespotlightc: prefManager.getBoolPref("extensions.TurnOfftheLights.mousespotlightc"), videodefault: prefManager.getBoolPref("extensions.TurnOfftheLights.videodefault"), videocloud: prefManager.getBoolPref("extensions.TurnOfftheLights.videocloud"), videocloudfile: prefManager.getCharPref("extensions.TurnOfftheLights.videocloudfile"), expirationdate: prefManager.getCharPref("extensions.TurnOfftheLights.expirationdate"), nighttime: prefManager.getBoolPref("extensions.TurnOfftheLights.nighttime"), begintime: prefManager.getCharPref("extensions.TurnOfftheLights.begintime"), endtime: prefManager.getCharPref("extensions.TurnOfftheLights.endtime"), cloudfileversion: prefManager.getCharPref("extensions.TurnOfftheLights.cloudfileversion"), addvideobutton: prefManager.getBoolPref("extensions.TurnOfftheLights.addvideobutton"), likebar: prefManager.getBoolPref("extensions.TurnOfftheLights.likebar"), ambilight: prefManager.getBoolPref("extensions.TurnOfftheLights.ambilight"), ambilightrangeblurradius: prefManager.getCharPref("extensions.TurnOfftheLights.ambilightrangeblurradius"), ambilightrangespreadradius: prefManager.getCharPref("extensions.TurnOfftheLights.ambilightrangespreadradius"), mousespotlightt: prefManager.getBoolPref("extensions.TurnOfftheLights.mousespotlightt"), ambilightfixcolor: prefManager.getBoolPref("extensions.TurnOfftheLights.ambilightfixcolor"), ambilightvarcolor: prefManager.getBoolPref("extensions.TurnOfftheLights.ambilightvarcolor"), ambilightcolorhex: prefManager.getCharPref("extensions.TurnOfftheLights.ambilightcolorhex"), ambilight4color: prefManager.getBoolPref("extensions.TurnOfftheLights.ambilight4color"), ambilight1colorhex: prefManager.getCharPref("extensions.TurnOfftheLights.ambilight1colorhex"), ambilight2colorhex: prefManager.getCharPref("extensions.TurnOfftheLights.ambilight2colorhex"), ambilight3colorhex: prefManager.getCharPref("extensions.TurnOfftheLights.ambilight3colorhex"), ambilight4colorhex: prefManager.getCharPref("extensions.TurnOfftheLights.ambilight4colorhex"), password: prefManager.getBoolPref("extensions.TurnOfftheLights.password"), enterpassword: prefManager.getCharPref("extensions.TurnOfftheLights.enterpassword"), noflash: prefManager.getBoolPref("extensions.TurnOfftheLights.noflash"), hardflash: prefManager.getBoolPref("extensions.TurnOfftheLights.hardflash"), ecosaver: prefManager.getBoolPref("extensions.TurnOfftheLights.ecosaver"), ecosavertime: prefManager.getCharPref("extensions.TurnOfftheLights.ecosavertime"), dynamic: prefManager.getBoolPref("extensions.TurnOfftheLights.dynamic"), dynamic1: prefManager.getBoolPref("extensions.TurnOfftheLights.dynamic1"), dynamic2: prefManager.getBoolPref("extensions.TurnOfftheLights.dynamic2"), dynamic3: prefManager.getBoolPref("extensions.TurnOfftheLights.dynamic3"), dynamic4: prefManager.getBoolPref("extensions.TurnOfftheLights.dynamic4"), dynamic5: prefManager.getBoolPref("extensions.TurnOfftheLights.dynamic5")});
         }
         else if ( request.name == "automatic" ) {
            var tab = documentToTab(gBrowser.contentDocument);
            firefox.tabs.executeScript(sender.tab, {file: "chrome://TurnOfftheLights/content/js/light.js"});
         }
         else if ( request.name == "readersaveme" ) {
            Services.prefs.setCharPref("extensions.TurnOfftheLights.interval", request.value);
         }
         else if ( request.name == "readerlargestyle" ) {
            Services.prefs.setBoolPref("extensions.TurnOfftheLights.readerlargestyle", request.value);
         }
        });
    };
   
    ns.onToolbarButtonClick = function() {
        var tab = documentToTab(gBrowser.contentDocument);
        firefox.tabs.executeScript(tab, {file: "chrome://TurnOfftheLights/content/js/light.js"});
    };
   
    ns.shortcut = function() {
      var shortcutlight = prefManager.getBoolPref("extensions.TurnOfftheLights.shortcutlight");
      if (shortcutlight) {
         var tab = documentToTab(gBrowser.contentDocument);
         firefox.tabs.executeScript(tab, {file: "chrome://TurnOfftheLights/content/js/light.js"});
      }
    };
   
    ns.keytheater = function() {
        var tab = documentToTab(gBrowser.contentDocument);
      firefox.tabs.sendRequest(tab, {name: "theater"});
    };
   
    var documentToTab = function(doc) {
        return {
            document: doc,
            url: doc.location.href
        }
    };

    var evalInSandbox = function(code, codebase, sandbox ){
        if (Components.utils && Components.utils.Sandbox) {
            // DP beta+
            Components.utils.evalInSandbox(code, sandbox);
        } else if (Components.utils && Components.utils.evalInSandbox) {
            // DP alphas
            Components.utils.evalInSandbox(code, codebase, sandbox);
        } else if (Sandbox) {
            // 1.0.x
            evalInSandbox(code, sandbox, codebase);
        } else {
            throw new Error("Could not create sandbox.");
        }
    };

    var getFileContent = function( file ) {
        var ioService=Components.classes["@mozilla.org/network/io-service;1"]
            .getService(Components.interfaces.nsIIOService);
        var scriptableStream=Components
            .classes["@mozilla.org/scriptableinputstream;1"]
            .getService(Components.interfaces.nsIScriptableInputStream);
        var unicodeConverter=Components
            .classes["@mozilla.org/intl/scriptableunicodeconverter"]
            .createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
        unicodeConverter.charset="UTF-8";

        var channel=ioService.newChannel( file, "UTF-8", null);
        var input=channel.open();
        scriptableStream.init(input);
        var str=scriptableStream.read(input.available());
        scriptableStream.close();
        input.close();

        try {
            return unicodeConverter.ConvertToUnicode(str);
        } catch (e) {
            return str;
        }
    };

    var console = {};
    console._service = Components.classes["@mozilla.org/consoleservice;1"]
        .getService(Components.interfaces.nsIConsoleService);
    console.log = function(msg) {
        console._service.logStringMessage(msg);
    };

    var firefox = {};
    firefox.tabs = {};
    firefox.tabs.executeScript = function(tab, details) {
        var unsafeWin = tab.document.defaultView;
      //unsafeWin.__exposedProps__ = { unsafeWin: "wr" }; // not needed
        var safeWin = new XPCNativeWrapper(unsafeWin);
      //safeWin.__exposedProps__ = { safeWin: "wr" }; // not needed
      //var sandbox = new Components.utils.Sandbox(safeWin);
      var systemPrincipal = Cc["@mozilla.org/systemprincipal;1"]  // fix for __exposedProps__
                              .createInstance(Ci.nsIPrincipal);
        var sandbox = new Components.utils.Sandbox( systemPrincipal );
        sandbox.window = safeWin;
        sandbox.document = sandbox.window.document;
        sandbox.unsafeWindow = unsafeWin;
        sandbox.__proto__ = sandbox.window;
        sandbox.firefox = {};
        sandbox.firefox.extension = new firefox.tabs.extension(tab.document);
        sandbox.console = console;
        var code = (typeof details.code != "undefined"
                    ? details.code
                    : ( typeof details.file != "undefined" ? getFileContent(details.file) : undefined ) );
        if ( code ) {
            try {
                evalInSandbox( code, tab.url, sandbox );
            } catch ( e ) {
                console.log("[ERROR] " + e );
            }
        }
    };

    firefox.tabs.event_id = ns.extension_id+".tabs.Request";
    firefox.tabs.request = {};
   
    firefox.tabs.sendRequest = function(tab, request, requestCallback) {
        var evt = tab.document.createEvent("Events");
        evt.initEvent(firefox.tabs.event_id, true, false);
        firefox.tabs.request = {
            data: request,
            callback: requestCallback
        };
        tab.document.dispatchEvent(evt);
    };

    firefox.tabs.extension = function(doc) {
        this.document = doc;
        this.event_id = firefox.tabs.event_id;

        var listeners = [];
        var handler = function(evt) {
            var request = firefox.tabs.request;
            for ( var i=0; i<listeners.length; i++ ) {
                listeners[i](request.data, null, request.callback);
            }
        };

        this.onRequest = {
         __exposedProps__: {addListener: "rw", removeListener: "rw"},
            addListener: function(listener) {
                listeners.push(listener);
            },
            removeListener: function(listener) {
                for ( var i=0; i<listeners.length; i++ ) {
                    if ( listener == listeners[i] ) {
                        listeners.splice(i, 1);
                    }
                }
            }
        };
        doc.addEventListener(firefox.tabs.event_id, handler, false, true );
    };
   
    var listeners = [];
    firefox.tabs.extension.prototype.sendRequest = function(request, responseCallback) {
        if ( typeof responseCallback!="function" ) {
            responseCallback = function() {};
        }
        for ( var i=0; i<listeners.length; i++ ) {
            listeners[i](request, { tab: documentToTab(this.document) }, responseCallback);
        }
    };

    firefox.extension = {};
    firefox.extension.onRequest = {
      __exposedProps__: {addListener: "rw", removeListener: "rw"},
        addListener: function(listener) {
            listeners.push(listener);
        },
        removeListener: function(listener) {
            for ( var i=0; i<listeners.length; i++ ) {
                if ( listener == listeners[i] ) {
                    listeners.splice(i, 1);
                }
            }
        }
    };
})(TurnOfftheLights);

window.addEventListener("load", function() {TurnOfftheLights.init();}, false);
<<

jorge-villalobos

AMO Administrator

Posts: 2990

Joined: Tue Sep 29, 2009 7:30 pm

Location: San José, Costa Rica

Post Fri Sep 07, 2012 1:39 pm

Re: exposedProps

OK, so it looks like the problem was that you were running the script with the page principal, and that broke after the change in 15 somehow. You're now running it with the system principal, which is similar to just running the chrome script on the page. Since you're not sharing any objects with the page (I assume), no more action is required to have the script run.

Return to Extension Development

Who is online

Users browsing this forum: No registered users

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
CA Gen2 style designed by Vjacheslav Trushkin.