I'm messing around with chrome extensions, and I'm trying to inject some JS into the page. (I want to, for example, make the main page popup a box saying something). Here's my JS file:
//Function to be called:
function setText() {
var text = document.getElementById("titleinput").value;
var myCode="alert('Test');";
chrome.tabs.executeScript(null, {code:myCode});
}
//Makes it run above code when a button is pressed:
document.addEventListener('DOMContentLoaded', function () {
document.querySelector('button').addEventListener('click', setText);
});
And here's my manifest file:
{
"name": "Tester",
"version": "1.0",
"manifest_version": 2,
"description": "description",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"tabs", "http://*/*", "https://*/*"
]
}
I know the setText()
is being called (I tried putting an alert directly in there), but for some reason the main page isnt making a popup saying 'Test' like it should. What am I doing wrong?
Make sure you are running it on a website and not chrome://extensions
. You can't inject scripts into chrome://*
pages. If it still isn't working use the popup debugger to look for errors.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.