Reputation: 1360
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?
Upvotes: 0
Views: 1688
Reputation: 29092
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.
Upvotes: 2