• Register
0 votes
706 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.

6 5 3
6,930 points

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

9 7 4
38,600 points
0 votes

Solution:

chrome:// urls are blocked for security causes. Google doesn't want you to alter the outlook or alter chrome settings without the user knowing it. At the time you load your extension, it instantly executes those files inside the chrome://extensions page. In case you want to execute your script in every tab the user goes to, you must 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
}); 

Generally (view also Programmatic Injection in docs) it's not possible to impel scripts into tabs with chrome:// urls since the approved schemes are <scheme> := '*' | 'http' | 'https' | 'file' | 'ftp'.

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

Substitutely you can enable chrome://flags/#extensions-on-chrome-urls flag, but this is hardly useful as Chrome will show a warning each begin.

You can also impulse 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 require any special permissions to do this.

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

10 6 4
31,120 points

Related questions

0 votes
1 answer 1.2K views
1.2K views
Problem : I am trying to build the Google Chrome extension. I searched on every platform and took every step to resolve my below error &ldquo;Your file was not found it may have been moved or deleted. err_file_not_found&rdquo; I have done everything that I could , but ... \User Data**),like the other extensions my extension also has no folder. Please let me know how to fix the extension issue.
asked Jan 22 jwilliam 3.9k points
0 votes
2 answers 1.2K views
1.2K views
Problem : Currently I am using VueJS and Laravel for the project. Following issue started to show lately and it also comes up even in the old git branches. Unchecked runtime.lasterror: the message port closed before a response was received. This error only comes up in Chrome browser.
asked Dec 10, 2019 alecxe 7.5k points
0 votes
1 answer 162 views
162 views
Problem : Facing following chrome error error in event handler for (unknown): typeerror: cannot read property 'indexof' of undefined
asked Nov 16, 2019 peterlaw 6.9k points
1 vote
1 answer 33 views
33 views
Problem: I have written a very simple application. Please find below the code for my simple application which is calling the API in each second. After each of the call a chrome memory allocation size for that particular tab increases continuously. But not by the reducing that memory. ... xhttp.send();     } </script> </body> </html> Kindly help me in finding out the root cause of this issue.
asked May 27 Martin K 6.6k points