Programmers learn & share
0 votes
253 views

Problem :

I am very new to chrome extension development.

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

While trying to run a background script using the chrome.tabs.executeScript() but it gives me following errors :

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

Please find below the code for your reference:

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;

});

Please note my auto.js is a precompiled js file which works perfectly fine when used alone in a html file. The main aim of my extension is to provide auto complete while writing in a text field.

by (6.9k points)   | 253 views

2 Answers

0 votes

Solution :

Please note the chrome:// urls are blocked for security reasons. Google will not want you to change the appearace or settings without the user knowing it. So when you load your extension, it will immediately executes those files inside the chrome://extensions page. So if you want to execute your script in every tab the user goes to, then you should use the following way:

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
}); 

 

It will solve your issue

by (36.1k points)  
0 votes

Solution:

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
}); 

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.com/_/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.com/_/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.

You can also inject code into pages packaged with your own extension:

browser.tabs.create({url: "/my-page.html"}).then(() => {
  browser.tabs.executeScript({
    code: `console.log('location:', window.location.href);`
  });
});

You don't need any special permissions to do this.

You cannot inject code into any of the browser's built-in pages, such as: about:debuggingabout:addons, or the page that opens when you open a new empty tab.

 

ago by (12.8k points)  
2,245 questions
2,807 answers
60 comments
241 users