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

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -