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?

cdm
  • 1,360
  • 11
  • 18
  • 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. – Pogrindis May 03 '16 at 10:37
  • Also I must refer you to here : http://stackoverflow.com/questions/24600495/chrome-tabs-executescript-cannot-access-a-chrome-url – Pogrindis May 03 '16 at 10:40

1 Answers1

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.

Community
  • 1
  • 1
CherryDT
  • 25,571
  • 5
  • 49
  • 74