最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - tabs.executeScript: Cannot access a chrome: URL - Stack Overflow

matteradmin6PV0评论
chrome.tabs.executeScript(null, { file: "jquery.js" }, function() {
chrome.tabs.update(null, {url: '/'});
});

above code work when I trigger when my address bar have something, means I'm at any web pages, but when I trigger when my address bar is blank, I got below error :

Unchecked runtime.lastError while running tabs.executeScript: Cannot access a chrome:// URL
    at Object.callback 
chrome.tabs.executeScript(null, { file: "jquery.js" }, function() {
chrome.tabs.update(null, {url: 'https://example./'});
});

above code work when I trigger when my address bar have something, means I'm at any web pages, but when I trigger when my address bar is blank, I got below error :

Unchecked runtime.lastError while running tabs.executeScript: Cannot access a chrome:// URL
    at Object.callback 
Share Improve this question asked May 21, 2015 at 7:32 Aaron MusktinAaron Musktin 3074 silver badges12 bronze badges 9
  • That's a strange logic to begin with: executeScript, then update the tab. What are you achieving here? – Xan Commented May 21, 2015 at 8:00
  • @Xan why? I inject jquery.js and my content script, to munite with the DOM, then I want to continue to do something with my popup.js. Above code worked when my tab loaded some web pages, not when the tab is blank. – Aaron Musktin Commented May 22, 2015 at 0:52
  • Well, in your simplified code you inject jQuery only, and even then your content script only has time for synchronous operations; anything async will enter a race condition with the tab navigating away and the content script will be wiped as soon as the navigation mits. – Xan Commented May 22, 2015 at 7:24
  • @Xan I don't get the async and sync part, are you saying there might be a problem with my flow above? – Aaron Musktin Commented May 22, 2015 at 7:27
  • Yes! This is precisely what I'm saying. The content script won't persist after navigation, and has little time to do anything before. Whether it succeeds or not depends on what you're doing. – Xan Commented May 22, 2015 at 7:31
 |  Show 4 more ments

1 Answer 1

Reset to default 2
  • Normally (see also Programmatic Injection in docs) it's not possible to inject scripts into tabs with chrome:// urls because the allowed schemes are <scheme> := '*' | 'http' | 'https' | 'file' | 'ftp'.

    In Chrome before v61 it was still possible to inject into the content frame of the New Tab page, where the "blank address bar" you mentioned is represented internally as chrome://newtab/. For example the main frame has an address like this: https://www.google./_/chrome/newtab?espv=2&es_th=1&ie=UTF-8 (use Network panel in devtools to inspect the urls). So your manifest.json would have "permissions": ["tabs", "https://www.google./_/chrome/newtab*"],

  • Alternatively you can enable chrome://flags/#extensions-on-chrome-urls flag, however this is hardly useful as Chrome will show a warning each start.

Post a comment

comment list (0)

  1. No comments so far