javascript google chrome extension getting domain name -
i tried domain name using alert(document.domain);
i'm not getting right domain when test out in site,
i "hiecjmnbaldlmopbbkifcelmaaalcfib" weird output.
i have added in manifest too
"content_scripts": [ { "js": ["inject.js"] } ],
alert(document.domain); line of text within inject.js.
and i've incorporated <script type="text/javascript" src="inject.js"> </script>
main html file after popup.js
any thoughts on why i'm not getting right domain url?
thanks!
if in popup or background or options page, there indirect approach obtaining domain of page.
you can refer next code reference.
demonstration manifest.jsonregistered content scripts, background , popup scripts manifest file along relevant permissions
{ "name": "domain name", "description": "http://stackoverflow.com/questions/14796722/javascript-google-chrome-extension-getting-domain-name", "version": "1", "manifest_version": 2, "content_scripts": [ { "matches": [ "<all_urls>" ], "js": [ "myscript.js" ] } ], "browser_action": { "default_popup": "popup.html" }, "background": { "scripts": [ "background.js" ] }, "permissions": [ "tabs", "<all_urls>" ] }
myscript.js console.log(document.domain);// outputs nowadays active url of tab
popup.html registered popup.js
surpass csp.
<html> <head> <script src="popup.js"></script> </head> <body></body> </html>
popup.js added event listener dom content loaded
, , brought active url of tab user on.
document.addeventlistener("domcontentloaded", function () { console.log(document.domain);//it outputs id of extension console chrome.tabs.query({ //this method output active url "active": true, "currentwindow": true, "status": "complete", "windowtype": "normal" }, function (tabs) { (tab in tabs) { console.log(tabs[tab].url); } }); });
background.js console.log(document.domain); //it outputs id of extension console chrome.tabs.query({ //this method output active url "active": true, "currentwindow": true, "status": "complete", "windowtype": "normal" }, function (tabs) { (tab in tabs) { console.log(tabs[tab].url); } });
output you find
fgbhocadghoeonlokakijhnlplgkolbg
as output console.log(document.domain); in extension pages ,
and
http://somedomain.com/
for tabs.query()
output.
however, content script output
http://somedomain.com/
references tabs api content scripts javascript google-chrome google-chrome-extension cross-domain
No comments:
Post a Comment