0

I am trying to develop a Chrome extension to open a new tab on button click and then execute js in it.

Manifest:

{
 "author": "...",
 "name": "...",
 "manifest_version": 2,
 "version": "1.0",
 "description": "...",
 "background": {},
 "browser_action": {
 "default_popup": "popup.html"
 },
 "permissions": [
 "tabs","http://*/","https://*/"
 ]
}

popup.html:

<html>
<head>
 <script src="jquery.min.js"></script>
</head>
<body>
 <button type="button" id="saveBtn">Save</button>
 <script type="text/javascript" src="popup.js"></script>
</body>
</html>

popup.js:

$("#saveBtn").click(function(){
 chrome.tabs.create({
 selected:false, 
 url:'https://analytics.google.com/something...'
 },function(tab){
 chrome.tabs.executeScript(tab.ib,{file:"jquery.min.js"});
 chrome.tabs.executeScript(tab.ib,{file:"html2canvas.js"});
 chrome.tabs.executeScript(tab.ib,{file:"FileSaver.js"});
 chrome.tabs.executeScript(tab.id,{file:'inject.js'});
 });
});

All javascript files are in the same folder. I am getting this error

Unchecked runtime.lastError while running tabs.executeScript: Cannot access a chrome:// URL at Object.callback (chrome-extension://jamfjopkccgnbpkhafanifhjambepphc/popup.js:8:23)

on lines where I am trying to inject jquery.min.js, html2canvas.js and FileSaver.js BUT NOT inject.js! New tab opens correctly, callback is executed but then the error is thrown. What am I doing wrong here?

asked May 3, 2016 at 10:35
2
  • Just so we are clear, your URL is correct and not https://analytics.google.com/something... ? Because it looks like you're trying to hit a chrome:// URL : Which you cannot do. Commented May 3, 2016 at 10:37
  • Also I must refer you to here : stackoverflow.com/questions/24600495/… Commented May 3, 2016 at 10:40

1 Answer 1

2

You have a typo in there.

chrome.tabs.executeScript(tab.ib,{file:"jquery.min.js"});
chrome.tabs.executeScript(tab.ib,{file:"html2canvas.js"});
chrome.tabs.executeScript(tab.ib,{file:"FileSaver.js"});
chrome.tabs.executeScript(tab.id,{file:'inject.js'});

The first three have tab.ib instead of tab.id, which will pass undefined. In this case, Chrome will try to run the script in your current tab, which is probably chrome://extensions or something like this.

From the docs:

integer (optional) tabId
The ID of the tab in which to run the script; defaults to the active tab of the current window.

By the way, you might want to take a look at this question to ensure that your scripts are executed in the right order.

answered May 3, 2016 at 10:54
Sign up to request clarification or add additional context in comments.

1 Comment

Yes! I would never notice that ib :D

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.