N
N
NeTLeaDeR2018-08-17 15:46:07
JavaScript
NeTLeaDeR, 2018-08-17 15:46:07

How to work with content script in chrome extension?

Hello. I decided to figure out how to write browser extensions, searched the Internet, but did not understand a little how to work with a content script. As far as I understand, this script is executed as if it is in the web page itself. But for some reason, for example, the simplest alert(); is not performed. Where is the mistake?
Code:
manifest.json

{
    "manifest_version": 2,
 
    "name": "name",
    "description": "description",
    "version": "1.0",
 
    "browser_action": {
        "default_popup": "popup.html"
    },

   "permissions":
    [
        "http://www.sitename.com/*"
    ],
    "content_scripts":[
        {
            "matches": [
                "http://www.sitename.com/*"
            ],
            "js": ["content_script.js"]
        }
    ]
}

popup.html:
<!doctype html>
<html>
    <head>
        <title>Title</title>
        <script src="popup.js"></script>
        <meta charset="utf-8">
    </head>
    <body>
        <h1>HeaderText</h1>
        <button id="start" OnClick="start()">Button</button>
    </body>
</html>

popup.js:
function start() {
    chrome.runtime.sendMessage('start');  
}

content_script.js:
chrome.runtime.onMessage.addListener(function(request,sender,callback){
    if(request=='start'){
        alert('aaaaaaa');
    }
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2018-08-17
@UPSA

6 hours and not a single offer ...
I can only offer .... because. as at one time I tried to only tweak a little
1) you need to pack the extension. Otherwise, it will not start due to the security of chrome.
2) Try not to alert ('aaaaaaa'), but not to insert a div directly into the body, i.e. the extension seems to affect the page....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question