1
votes

I am working on a Firefox Add, using the newest SDK. The problem I am having, is that when ever I try to run a custom dom function it doesn't work.

Scenario: The firefox add-on must be able to loop through all tabs, and if the correct one is open based on the title, run a specific function, like: mydom.checkIt();

<!DOCTYPE html>
<html>
<head>
   <title>My Web</title>
</head>
<body>

<script>
    mydom = {};
    mydom.checkIt = function (){
                          alert('Hi');
                    };
</script>

</body>
</html>

Then the add-on source code would be something like:

var tabs = require('sdk/tabs');

for (let tab of tabs)
{
        if(tab.title=='My Web')
        {
            tab.activate();             
        }
}

tabs.on('activate', function(tab) {

  tab.attach({
    contentScript: "if(typeof(mydom)!=='undefined')mydom.checkIt();else console.log('no defined');"
  });


});

But this doesn't work and it says always: "no defined"

Any ideas?

1
You have to get into the dom js scope with unsafeWindow or wrappedJSObject. So contentWindow.wrappedJSOBject.checkIt() works from bootstrap addon (not sure about sdk) - Noitidart
put this as an answer so that I can accept it, Thanks! - peterpeterson

1 Answers

1
votes

You have to get into the dom js scope with unsafeWindow or wrappedJSObject. So contentWindow.wrappedJSOBject.checkIt() works from bootstrap addon (not sure about sdk)

Warning though, this may not be e10s friendly and we should find a way to support that