0

I am trying to create a jquery popup on any page, triggered on demand, when the user presses on my chrome extension.

I have permissions set to [ "tabs", "http:///", "https:///" ]

I have a background page which tries to do the following:

chrome.browserAction.onClicked.addListener(function(tab) {

//chrome.tabs.executeScript(null, { code: "alert(document.title);" }, null);

chrome.tabs.executeScript(null, {file: "demo.js"}, null);

chrome.tabs.executeScript(null, { code: "document.body.appendChild(document.createElement('script')).src='demo.js'" }, null);
      });

If I uncomment the alert, it appears when I click on the extension icon. But with the comment as it is it doesn't do anything.

Any thoughts why it fails?

UPDATE I managed to get it working, by referencing a url and not a local resource(demo.js). Now the code, that works, looks like this:

chrome.tabs.executeScript(tab.id, { code: "document.body.appendChild(document.createElement('script')).src='http://iamnotagoodartist.com/stuff/wikiframe.js'" }, null); 

My local "demo.js" was a copy of the content from that url anyway. I am not sure why it doesn't work when I reference the local file... ?

Andrei
  • 89
  • 2
  • 11

2 Answers2

0

You are not passing the tab id to executeScript so the scripts are getting injected into the backround page where you can't interact with them.

chrome.tabs.executeScript(tab, {file: "demo.js"}, null);

abraham
  • 46,583
  • 10
  • 100
  • 152
  • thank you. I added chrome.tabs.executeScript(tab.id, {file: "demo.js"}, null); chrome.tabs.executeScript(tab.id, { code: "document.body.appendChild(document.createElement('script')).src='demo.js'" }, null); Unfortunately, the same thing happens, nothing appears. – Andrei Nov 28 '11 at 14:20
  • if i make a demo html page in which I include Test then the popup appears. I'm trying to inject that code on demand, into any web page. – Andrei Nov 28 '11 at 14:22
  • awesome, thanks! (chrome.tabs.executeScript(null, { code: "document.body.appendChild(document.createElement('script')).src='"+chrome.extension.getURL("demo.js")+"'",allFrames:true }) – Andrei Nov 30 '11 at 08:39
0

You must use chrome.extension.getURL to get the full path to the "demo.js" file.

chrome.tabs.executeScript(null, { 
  code: "document.body.appendChild(document.createElement('script')).src='" + 
    chrome.extension.getURL("demo.js") +"';" 
}, null);

BTW if you set tabId parameter to null, the script won't be injected into the background page but into the selected tab of the current window.

check_ca
  • 51
  • 1
  • Awesome, thanks a lot! \n
    Do you know also how to be able to run it in iframe sites like gmail? I found the "all_frames": true reference but it's for content scripts which are always injected, while I only inject on demand.
    – Andrei Nov 29 '11 at 23:00
  • You just have to set allFrames attribute to true in the 2nd parameter object (see the second link). Note that you will not be able to inject scripts into frames created via JavaScript. – check_ca Nov 30 '11 at 00:45
  • tx a lot! i still get security warning in gmail, but it works – Andrei Dec 07 '11 at 22:04
  • 1
    You can also use `chrome.runtime.getURL`, and it looks like `chrome.extension.getURL` is falling out of favor. – skeggse Jun 13 '14 at 20:36
  • And you must enable this in manifest: `"web_accessible_resources": ["script.js"]` [See this answer](https://stackoverflow.com/a/10529675/5739006) and [docs](https://developer.chrome.com/extensions/manifest/web_accessible_resources) – WerWet Jul 06 '18 at 14:30