2

So I have an extension I'm writing and I'm trying to execute a script when the user clicks on the pageAction icon. When the icon is clicked, the method calls chrome.tabs.executeScript(...). The problem is that the chrome.tabs.executeScript function is not executing and I can't tell why. I know that I'm getting to the code where it calls executeScript because I have an alert there that appears. Here is some of my code:

manifest.json

{ "manifest_version": 2, "name": "name here", "description": "description here", "version": "0.1", "permissions": [ "<all_urls>", "tabs" ], "icons": { "16" : "images/icon16.png", "48" : "images/icon48.png", "128": "images/icon128.png" }, "background": { "scripts": ["js/check.js"] }, "page_action": { "default_icon": { "19": "images/icon19.png", "38": "images/icon38.png" }, "default_title": "default title here" } } 

js/check.js

chrome.tabs.onUpdated.addListener(checkForValidUrl); function checkForValidUrl(tabId, changeInfo, tab) { if (tab.url.indexOf('g') > -1) { chrome.pageAction.show(tabId); } }; chrome.pageAction.onClicked.addListener(function(tab) { alert("hello world"); //this code is executed... //...this code is not chrome.tabs.executeScript(tab.id, {file: "save.js"}, function() { if(chrome.runtime.lastError) { console.error(chrome.runtime.lastError.message); } }); }); 

js/save.js

alert("hello world"); 

Like I say in the code, the hello world in my pageAction onClick function works. The executeScript method does not. Any idea about what is going on would be helpful.

1
  • Also, the console.error(...) code that should fire after the executeScript(...) method returns is never called, so that hasn't helped me. Commented Jun 25, 2013 at 2:33

2 Answers 2

3

After messing around with lots of different things in my code, I've found the solution to my problem. The error seems to be in the line that says {file: "save.js"}. When it's looking for save.js, it's apparently looking in the top directory, where my manifest.json file is located, not in the directory that my code is in. I had to change my code to {file: "js/save.js"} in order for my save.js file to be found.

Sign up to request clarification or add additional context in comments.

Comments

2

According to the docs:

To insert code into a page, your extension must have cross-origin permissions for the page. It also must be able to use the chrome.tabs module. You can get both kinds of permission using the manifest file's permissions field.

So you need a permission for the site, i.e. http://example.com/ in the permission field.

6 Comments

I added the <all_urls> permission to the manifest just until I get my problem solved, but I'm still getting nothing. No code response or error being shown.
@mottese does the file exist? Does it contain errors? Any error in the developer console?
the file exists, all it contains is alert("hello world"); which contains no errors, and the developer console shows nothing, even though I have explicitly written code saying to print an error.
@mottese How about the developer console of the page containing check.js? (Usually it's the background page)
I've found the error. Apparently when I write file: "save.js", it is looking in top directory that contains my manifest file. I had to change it to file: "js/save.js" in order for it to find the file.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.