Chrome Extension - Simple Content Script for running js on any page

前端 未结 1 1889
情书的邮戳
情书的邮戳 2021-01-30 10:57

How can I write a simple Chrome Extension content script that will execute JavaScript (for example alert(\"hello\");) on every page load?

So when I navigate

1条回答
  •  礼貌的吻别
    2021-01-30 11:29

    If all you need is to alert hello on every page load or reload, below is a simple demo: Manifest.json:

    {
        "name": "Highlight some phrases",
        "description": "Hightlight some pre defined text after page loads",
        "version": "0.1",
        "permissions": [
            "tabs",""
        ],
        "browser_action": {
            "default_icon": "icon.png"
        },
        "content_scripts": [
            {
            "matches": [
                "http://*/*",
                "https://*/*"
                ],
            "js": ["content.js"],
            "run_at": "document_end"    // Pay attention to this line
            }
        ], 
        "manifest_version": 2
    }
    

    and content.js:

    // alert("hello");
    document.body.style.background = 'yellow';
    

    Yes, that's enough.
    And of course, don't forget to add an icon named icon.png at the same directory with these two files, then test it in Google Chrome.

    0 讨论(0)
提交回复
热议问题