Answer the question
In order to leave comments, you need to log in
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"]
}
]
}
<!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>
function start() {
chrome.runtime.sendMessage('start');
}
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
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 questionAsk a Question
731 491 924 answers to any question