admin管理员组文章数量:1314514
I just get started building my first Chrome extension. One functionality in my app is to get the name of the current site. For example, if the current url matches "*://*.facebook/", I would interpret this url and know the site name as Facebook.
How should I go about doing this?
I just get started building my first Chrome extension. One functionality in my app is to get the name of the current site. For example, if the current url matches "*://*.facebook./", I would interpret this url and know the site name as Facebook.
How should I go about doing this?
Share Improve this question edited Apr 21, 2015 at 23:37 Marco Bonelli 69.5k21 gold badges126 silver badges145 bronze badges asked Jan 11, 2015 at 7:49 hsiaomichiuhsiaomichiu 6026 silver badges22 bronze badges 6- Are you trying to do so in a content script ? – Omri Aharon Commented Jan 11, 2015 at 8:14
- Any way will do, although I think it's impossible to do it in a content script (since we don't have access to chrome.tabs there). What I'm thinking of doing is to send a message to the background and get back the solution. – hsiaomichiu Commented Jan 11, 2015 at 8:18
-
1
I think it should work if you try to access
window.location.host
from the content script, since the script is injected to that page – Omri Aharon Commented Jan 11, 2015 at 8:21 - Yup it works. Great yet simple solution. Thank you! – hsiaomichiu Commented Jan 11, 2015 at 8:29
- @OmriAharon Please make that into an answer. Avoid answering in ments - it cannot be marked as accepted, makes a bad question "track record" for the OP, and it's not obvious that the question is answered. – Xan Commented Jan 11, 2015 at 9:12
2 Answers
Reset to default 7It's not clear wheter you want to get the host name or the title of the page, but you can get both of them if you want.
Inside a content script you can do:
var site = location.hostname,
title = document.title;
alert("Site: " + site + " - Title: " + title);
If you want to do this from the background page and you don't want to use content scripts nor inject code in the tab then you can only get the host name:
var site;
chrome.tabs.query({/* some query */}, function(tabs) {
site = tabs[0].url.split("/")[2];
alert("Site: " + site);
});
If you also want to get the page title from the background page you'll need to use executeScript()
. NOTE: since January 2021, use Manifest V3 with chrome.scripting.executeScript()
instead of chrome.tabs.executeScript()
.
var title;
chrome.runtime.onMessage.addListener(function(message, sender, sensResponse) {
if (message.title) {
title = message.title;
alert("Title: " + title);
}
});
chrome.tabs.query({/* some query */}, function(tabs) {
chrome.tabs.executeScript(tabs[0].id, {code: "chrome.runtime.sendMessage({title: document.title});"});
});
For more informations about the methods I used in the above snippets you can see these documentation links:
chrome.tabs
APIchrome.tabs.query
methodchrome.tabs.executeScript
method (deprecated, manifest v2)
chrome.scripting.executeScript
method (manifes v3)- Message passing
chrome.runtime.onMessage
eventchrome.runtime.sendMessage
method
Since the content script is injected into the page, you could use window.location.host
in the content script itself and get the name of your site.
本文标签: javascriptChrome extension get current site nameStack Overflow
版权声明:本文标题:javascript - Chrome extension: get current site name - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741963250a2407403.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论