asamolion
asamolion

Reputation: 848

javascript - run chrome extension as soon as page loads

I wan't to run my chrome extension as soon as the web page the user is visiting completes loading.

The extension works fine if I click it but I want it to run automatically on page load.

Is there any way to do this?

Here is my manifest

{
    "manifest_version": 2,

    "name": "Simple Text Highlighter",
    "description": "This plugin notify of the word 'Simple' in the current page",
    "version": "1.0",

    "browser_action": {
        "default_icon": "icon.png",
        "default_popup": "popup.html"
    },

    "content_scripts": [{
        "matches": ["http://*/*", "https://*/*"],
        "js": ["jquery.min.js", "highlightSimple.js"],
        "css": ["highlight.css"],
        "run_at": "document_end"
    }],

    "permissions": [
        "tabs",
        "activeTab",
        "notifications",
        "http://*/",
        "https://*/"
    ]

}

and here is the JavaScript

(function () {
    chrome.tabs.executeScript(null, {
        file: 'highlight.css'
    }, function () {
        chrome.tabs.executeScript(null, {
            file: 'jquery.min.js'
        }, function () {
            chrome.tabs.executeScript(null, {
                file: 'replace.js'
            });
        });
    })
})();

Thanks in advance.

Upvotes: 0

Views: 232

Answers (1)

Haibara Ai
Haibara Ai

Reputation: 10897

You have set run_at field document_end, which means your scripts will be injected as soon as the page loads.

the files are injected immediately after the DOM is complete, but before subresources like images and frames have loaded.

And chrome.tabs.executeScript can only be called in extension context, it's not allowed in content scripts.

Upvotes: 1

Related Questions