javascript - Chrome Extensions: Passing message by button from popup.html to event page -
i open new tab within popup.html clicking button contained in popup.html.
my approach send message popup.html eventpage.js open new tab when message received.
this not working. please see code below:
popup.html
<!doctype html> <html style=''> <head> <script src="popup.js"></script> </head> <body style="width:400px;"> <div id="content"> <div id="opentab"><button type="button" id="btnopentab" name="btnopentab" onclick="sendmessage()">open new tab</button></div> </div> </body> </html>
eventpage.js
chrome.extension.onmessage.addlistener( function(request, sender, sendresponse) { if (request.action == "opennewtab") chrome.tabs.create({ url: request.url }); } );
popup.js
function sendmessage() { chrome.extension.sendmessage({ action: "opennewtab", url: "www.google.com" }); }
manifest.json
{ "name": "open new tab", "version": "1.0", "manifest_version": 2, "description": "opens new tab", "background": { "scripts": ["eventpage.js"], "persistent": false }, "browser_action": { "default_icon": "icon.png", "default_popup": "popup.html" }, "permissions": ["tabs", "<all_urls>"] }
edit: found error message in console message:
refused execute inline event handler because violates following content security policy directive: "script-src 'self' chrome-extension-resource:". either 'unsafe-inline' keyword, hash ('sha256-...'), or nonce ('nonce-...') required enable inline execution.
the content security policy prevents using javascript in html (in case, getting error because of onclick="sendmessage()"
in popup.html
file.
you can desired result removing onclick
attribute button , instead adding event listener in popup.js
file:
document.getelementbyid('btnopentab').onclick = sendmessage;
Comments
Post a Comment