1

Suppose I have boo function inside an external JavaScript file, from domain http://localhost/file.js:

file.js:

function boo() {
  return 1;
}

How can I call the boo function from content script in chrome extension?

Afshin Mehrabani
  • 33,262
  • 29
  • 136
  • 201

2 Answers2

3

You'll need to run a content script that creates and injects a <script> element that calls the page's boo() function.

Your content script should have something alone the lines of:

function runBoo() {
    var myBoo = document.createElement('script');
    myBoo.id = 'myBoo';  //helpful for removing the element later, not required to work
    myBoo.innerText = 'boo();'
    document.body.appendChild(myBoo); //if you plan on calling boo() multiple times, don't forget to delete the previously added myBoo elements
}

window.onload = function() {
//call runBoo() from content whenever you want to run the page's boo() function
   runBoo();
}
MeLight
  • 5,454
  • 4
  • 43
  • 67
1

According to this doc from Mozilla:

Content scripts can access the DOM of a page, of course, just like any scripts that the page has loaded (page scripts). But content scripts are insulated from page scripts:

  • content scripts don't see any JavaScript objects added to the page by page scripts
  • if a page script has redefined the behavior of some DOM object, the content script sees the original behavior.

Reasons:

  1. it means that content scripts don't leak objects to web pages, potentially opening up security holes.

  2. it means that content scripts can create objects without worrying about whether they might clash with objects added by page scripts.

Update

@Xan was right! thanks Xan. if you need to interact with a function added by other page script, messaging between content script of your add-on and the page script is like this:

// main.js

var tabs = require("sdk/tabs");
var mod = require("sdk/page-mod");
var self = require("sdk/self");

var pageUrl = self.data.url("page.html")

var pageMod = mod.PageMod({
  include: pageUrl,
  contentScript: "console.log(unsafeWindow.foo);"
})

tabs.open(pageUrl);

where foo is a variable added by a page script.

Reyraa
  • 4,174
  • 2
  • 28
  • 54
  • And yet, it's [perfectly possible](http://stackoverflow.com/questions/9515704/building-a-chrome-extension-inject-code-in-a-page-using-a-content-script). – Xan Aug 25 '14 at 08:23
  • Content Scripts live in an isolated world by design, this is true, but since they have access to the DOM, they can inject code via a ` – Xan Aug 25 '14 at 08:24
  • Even the doc you quote says: _"Usually the insulation between content scripts and page scripts is what you want. But sometimes you might want to interact with page scripts: you might want to share objects between content scripts and page scripts or to send messages between them. If you need to do this, read about interacting with page scripts."_ – Xan Aug 25 '14 at 08:26
  • @Xan the question in about how to call a function witch is injected by a page script, not how to inject or link a script. – Reyraa Aug 25 '14 at 08:29
  • Does executing `boo()` in the page context not constitute "calling"? – Xan Aug 25 '14 at 08:29
  • 1
    I suggest you read the other answer more carefully. I won't argue further. – Xan Aug 25 '14 at 08:31
  • 1
    `unsafeWindow` is Firefox-specific. The question is Chrome-specific. – Xan Aug 25 '14 at 09:28