我想通过chrome扩展程序在变量中使用url。 我在这里读到这段代码:
E
但它不适用于此网址
http://sail.zezo.org/clipperton-noumea/chart.pl?lat=-15.93361&lon=-176.0022&userid=1577252
的manifest.json:
dE/dy_i
我不明白我在哪里弄错了......
(抱歉我的英语不好......)
答案 0 :(得分:3)
好的,所以我写了完整的解决方案,以获取popup.js中的活动网址:
的manifest.json:
{
"manifest_version": 2,
"name": "Route zezo.org",
"version": "1.0",
"description": "Extraire la route proposée",
"permissions": [
"http://sail.zezo.org/*",
"tabs"
],
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["content.js"],
"run_at": "document_end"
}],
"icons": {
"16": "icon-16.png",
"48": "icon-48.png",
"128": "icon-128.png"
},
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"background": {
"scripts": ["background.js"]
}
}
popup.js
window.onload=function(){
chrome.runtime.getBackgroundPage(function(backgroundPage){
var currentUrl = backgroundPage.tabURL;
//Use the url ........
console.log(currentUrl);
})
};
background.js
var tabURL = "";
chrome.runtime.onMessage.addListener(
function(message, sender, sendResponse) {
chrome.tabs.query({active: true, currentWindow: true},
function(arrayOfTabs) {
var activeTab = arrayOfTabs[0];
tabURL = activeTab.url;
});
}
);
content.js
chrome.runtime.sendMessage("send");
这个想法:您的扩展程序是在您在清单中的' 中放入的任何网址中运行的。内容脚本将消息发送到您想要获取此URL的后台页面,后台页面获取消息并将其设置在变量中,然后在popup.js页面中获取此变量。现在你可以用这个变量做任何你想做的事。
祝你好运!