我有一个Chrome扩展程序,如果window.my_variable_name
存在变量,它会尝试查找每个浏览过的网址(以及每个浏览器网址的每个iframe).
所以我写了一小段内容脚本:
function detectVariable(){
if(window.my_variable_name || typeof my_variable_name !== "undefined") return true;
return false;
}
Run Code Online (Sandbox Code Playgroud)
在尝试太久之后,似乎Content Scripts在一些沙箱中运行.
有没有办法window
从Chrome内容脚本访问该元素?
我正在尝试实现我自己的chrome扩展,在某个特定事件上创建浏览器通知并使用background.js中计算的数据填充弹出窗口
这是mymanifest文件:
{
"name": "Dummy name",
"description": "Description",
"manifest_version": 2,
"version": "1.1.3",
"icons": {
"16": "icon_16.png",
"48": "icon_48.png",
"128": "icon_128.png",
"256": "icon_256.png"
},
"browser_action": {
"default_icon": "icon_48.png",
"default_title": "Test",
"default_popup": "popup.html"
},
"permissions": ["background","webRequest","webRequestBlocking","webNavigation","tabs","notifications"],
"background": {
"scripts":["jquery-1.8.1.min.js","classy.js","background.js"]
}
}
Run Code Online (Sandbox Code Playgroud)
我在background.js中调用sendMessage
show : function(result) {
var that = this;
chrome.extension.sendMessage({greeting: "hello"}, function(response) {
console.log(response);
});
if(window.webkitNotifications) {
var notification = webkitNotifications.createHTMLNotification('notification.html');
notification.show();
setTimeout(function(){
notification.cancel();
}, '7000');
}
}
Run Code Online (Sandbox Code Playgroud)
我在popup.js中的消息监听器(来自chrome扩展示例)
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + …
Run Code Online (Sandbox Code Playgroud) messaging google-chrome message-passing google-chrome-extension