30

I am pretty new to chrome extension development.

The issue is not with accessing the chrome:// url I do not want to edit anything there, but the issue is regarding the execution of the chrome.tabs.executeScript() which is used for injection of the scripts.

I am trying to run a background script using the chrome .tabs.executeScript but it gives the following errors :


Unchecked runtime.lastError while running tabs.executeScript: Cannot access a chrome:// URL

I have the following code :

Manifest

{
    "name": "BrowserExtension",
    "version": "0.0.1",
    "manifest_version": 2,
    "description" : "Description ...",
    "icons": { "16": "icons/16x16.png", "48": "icons/48x48.png", "128": "icons/128x128.png" },
    "background" : {
        "scripts": ["background.js"]
    },      
    "permissions": [
        "tabs",
        "background",
        "http://*/*",
        "https://*/*"
    ],
    "browser_action": {
        "default_icon": {
            "19": "icons/19x19.png",
            "38": "icons/38x38.png"
        },
        "default_title": "That's the tool tip"
    }   
}

Background.js

console.log("background.js : click()");
chrome.tabs.executeScript(null, {file: "jquery.min.js"}, function(){
    chrome.tabs.executeScript(null, {file: "auto.js"}, function(){
        chrome.tabs.executeScript(null, {file: "script.js"}, function(){
            //all injected
        });
    });
});

script.js

$(function()
{
    var input = $('input');
    $.each(input,function(index,element){
        var area = new AutoSuggestControl(element.id);
    });    

    var ta = $('textarea');
    $.each(ta,function(index,element){ var area = new AutoSuggestControl(element.id);});

    return 1;
});

auto.js is a precompiled js file which works perfectly fine when used alone in a html file. The aim of the extension is to provide autocomplete while writing in a textfield. Thanks a ton for helping.

Marek Dorda
  • 1,255
  • 17
  • 19
slayer
  • 413
  • 1
  • 4
  • 9
  • 7
    `chrome.tabs.executeScript` does not run a background script, but a [content script](https://developer.chrome.com/extensions/content_scripts). Your code attempts to run on `chrome://extensions/` when loaded in unpacked mode because that is the last active tab. If you're not asking about why you cannot access `chrome://`-URLs, edit the question and title, and state more precisely what you have tried, what you expected, but what happened instead and I will re-open the question. – Rob W Jul 08 '14 at 19:26

1 Answers1

37

chrome:// urls are blocked for security reasons. Google doesn't want you to change the appearace or change chrome settings without the user knowing it. when you load your extension, it immediately executes those files inside the chrome://extensions page. If you want to execute your script in every tab the user goes to, you should use:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    //code in here will run every time a user goes onto a new tab, so you can insert your scripts into every new tab
}); 
Marc Guiselin
  • 3,442
  • 2
  • 25
  • 37
  • 1
    Catch the error using here: https://stackoverflow.com/a/45603880/632951 – Pacerier Aug 10 '17 at 03:33
  • Does not work in Chrome Version 76.0.3809.132 (Official Build) (64-bit). When you try to modify a chrome page, you get the same error in the event handler as you get doing it directly. I was hoping to change styles in Chrome Distiller, but no dice. – George Sep 19 '19 at 02:16
  • 1
    This answer explains the error, but does not explain why chrome:// URLs match http:// and https:// permissions, nor how chrome.tabs.onUpdated.addListener() avoids chrome:// URLs. – Graham Leggett Sep 16 '22 at 23:03