• Register
0 votes
1.7k 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

Please log in or register to answer this question.

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 2 views
2 views
Problem: Is there an efficient way to resolve this > How to stop chrome from opening new tabs when clicking a link
asked 1 day ago ummeshani 10.8k points
0 votes
1 answer 7 views
7 views
Question: How can I go about redirecting chrome in an extension when visiting a given URL? For example: when I visit http://yahoo.com/ I want it to redirect to http://google.com/ NOTE: A former version of this question asked whether there ... extension which automatically redirects the tab when it visits a certain URL. Accordingly, the (currently two) answers below address different questions.
asked Apr 18 munim01 21k points
0 votes
1 answer 7 views
7 views
Problem: I'm using VueJS and Laravel for my project. This issue started to show lately and it shows even in the old git branches. This error only shows in Chrome browser.
asked Apr 26 ochena 23.3k points
0 votes
2 answers 2.6K views
2.6K 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 11 views
11 views
Problem: Please let me know any solution regarding this .. How to open a duplicate tab in chrome
asked Apr 27 Ifra 37.2k points
0 votes
1 answer 4 views
4 views
Problem: Can someone give me a step by step tutorial of how to make a Google Chrome Extension?
asked Apr 18 muktaa 34.6k points
1 vote
1 answer 6 views
6 views
Problem: Any solution for this I am facing it while exporting to pdf in chrome: Err_response_headers_multiple_content_disposition chrome.
asked Apr 27 PkGuy 23.5k points
0 votes
1 answer 5 views
5 views
Problem: Please can anyone tell me the right procedure to do it? Chrome extension send message from background to popup
asked Apr 26 Ifra 37.2k points
0 votes
1 answer 6 views
6 views
Problem: I've got system.display in permissions for my manifest.json and once I've create a window I open up the console and type in the following: chrome.system.display.getInfo(function(displayInfo) { console.log(displayInfo); }); If I just type in chrome.system.display in the ... an object (which it didn't before I put in the permission in the manifest.json) so I'm not sure what I'm missing.
asked Apr 22 nabin shrestha 4.3k points
0 votes
1 answer 4 views
4 views
Problem: While I was developing a chrome extension, when the extension shows as a new browser window all the browser toolbars (back, refresh, bookmarks, etc.) are visible. I've seen extensions that open with this method, but don't have their toolbars. Does anyone know how to remove the browser toolbars and show a stripped down window?
asked Apr 22 ochena 23.3k points