最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript google chrome extension getting domain name - Stack Overflow

programmeradmin0浏览0评论

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 outputting chrome-extension://(the app id)/popup.html? – hellomello Commented Feb 10, 2013 at 20:14
Add a ment  | 

1 Answer 1

Reset to default 7

If 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
发布评论

评论列表(0)

  1. 暂无评论