admin 管理员组文章数量: 1086019
I tried to get the domain name using alert(document.domain);
But i'm not getting the right domain when I test it out in a site,
I get "hiecjmnbaldlmopbbkifcelmaaalcfib" this weird output.
I have added this in the manifest too
"content_scripts": [
{
"js": ["inject.js"]
}
],
alert(document.domain); is the only line of text inside inject.js.
And I've incorporated this <script type="text/javascript" src="inject.js">
</script>
into the main html file after popup.js
Any thoughts on why I'm not getting the correct domain url?
Thanks!
I tried to get the domain name using alert(document.domain);
But i'm not getting the right domain when I test it out in a site,
I get "hiecjmnbaldlmopbbkifcelmaaalcfib" this weird output.
I have added this in the manifest too
"content_scripts": [
{
"js": ["inject.js"]
}
],
alert(document.domain); is the only line of text inside inject.js.
And I've incorporated this <script type="text/javascript" src="inject.js">
</script>
into the main html file after popup.js
Any thoughts on why I'm not getting the correct domain url?
Thanks!
Share Improve this question asked Feb 10, 2013 at 10:22 hellomellohellomello 8,60742 gold badges154 silver badges310 bronze badges 4-
what about
window.top.location
? – Eliran Malka Commented Feb 10, 2013 at 10:31 - 3 The 'weird' output is the id of your app, what domain value are you expecting? – lostsource Commented Feb 10, 2013 at 10:33
- 1 @lostsource stackoverflow, latimes, yahoo, etc... all sorts of domain names – hellomello Commented Feb 10, 2013 at 20:12
-
@EliranMalka for some reason
window.top.location
keeps outputtingchrome-extension://(the app id)/popup.html
? – hellomello Commented Feb 10, 2013 at 20:14
1 Answer
Reset to default 7If you are in popup or background or options page, there is an indirect approach for obtaining domain of page.
You can refer to following code as a reference.
Demonstration
manifest.json
Registered content scripts, background and popup scripts with manifest file along with relevant permissions
{
"name": "Domain Name",
"description": "http://stackoverflow./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 present active URL of tab
popup.html
Registered popup.js
to surpass CSP.
<html>
<head>
<script src="popup.js"></script>
</head>
<body></body>
</html>
popup.js
Added Event Listener for DOM Content Loaded
, and brought active URL of tab where user is on.
document.addEventListener("DOMContentLoaded", function () {
console.log(document.domain);//It outputs id of extension to console
chrome.tabs.query({ //This method output active URL
"active": true,
"currentWindow": true,
"status": "plete",
"windowType": "normal"
}, function (tabs) {
for (tab in tabs) {
console.log(tabs[tab].url);
}
});
});
background.js
console.log(document.domain); //It outputs id of extension to console
chrome.tabs.query({ //This method output active URL
"active": true,
"currentWindow": true,
"status": "plete",
"windowType": "normal"
}, function (tabs) {
for (tab in tabs) {
console.log(tabs[tab].url);
}
});
Output
You will find
fgbhocadghoeonlokakijhnlplgkolbg
as output for console.log(document.domain); in all extension pages and
and
http://somedomain./
for tabs.query()
output.
However, Content script output is always
http://somedomain./
References
- Tabs API
- Content Scripts
本文标签: javascript google chrome extension getting domain nameStack Overflow
版权声明:本文标题:javascript google chrome extension getting domain name - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744050584a2524943.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论