我开发了当前页面网址的一个chrome扩展基础。 下面列出了我的chrome扩展文件。
我的manifest.json文件是
{
"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",
"default_icon": {
"38": "icon.png"
}
},
"background": {
"scripts": [
"background.js"
]
},
"permissions": [
"tabs",
"<all_urls>"
],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["myscript.js","popup.js"]
}
]
}
我的background.js文件
chrome.tabs.onUpdated.addListener(function (tabId, change, tab) {
if (tab.url == "https://www.facebook.com/") {
chrome.browserAction.setIcon({ path: 'BGicon.png' });
}
else {
chrome.browserAction.setIcon({ path: 'icon.png' });
}
});
myscript.js
console.log(document.domain);
var domainURL = document.domain;
if (document.domain == "facebook.com") {
alert("Succes");
}
popup.js
document.addEventListener("DOMContentLoaded", function () {
console.log(document.domain);//It outputs id of extension to console
renderStatus('Search term: Google image search result: ');
chrome.tabs.query({ //This method output active URL
"active": true,
"currentWindow": true,
"status": "complete",
"windowType": "normal"
}, function (tabs) {
for (tab in tabs) {
console.log(tabs[tab].url);
}
});
});
function renderStatus(statusText) {
document.getElementById('status').textContent = statusText;
}
我想按打开页面设置msg文本。我的意思是如果我打开facebook.com然后弹出消息设置并显示,否则弹出消息不会显示。