谷歌浏览器 - 扩展铬空闲不起作用 [英] Google chrome - extension chrome idle not working
问题描述
获取错误:未捕获TypeError:无法读取属性'queryState'的未定义
如何运行从我的代码扩展(
使用扩展程序打开网站并不奇怪地授予网站使用Chrome扩展API的权限。
-
最简单的方法是将
index.html
作为扩展程序的一部分文件。然后,您可以使用该API,但请注意 Chrome的CSP (您不能使用内嵌脚本)。 / p>
有很多方法可以使用为此,例如 externally_connectable
,或通过上下文脚本和共享DOM进行交谈。
无论如何: chrome.idle
只能从扩展自己的页面调用,包括但不限于背景页面。
另外,请使用 chrome.windows.create
或 chrome.tabs.create
而不是 wi ndow.open
在扩展名中。它不需要特殊的权限。
Getting ERROR: Uncaught TypeError: Cannot read property 'queryState' of undefined
how can i run the extension (https://developer.chrome.com/apps/idle ) from my code?
manifest.json:
{
"name" : "Idle - Simple Example",
"version" : "1.0.1",
"description" : "Demonstrates the Idle API",
"background" : {
"scripts": ["background.js"]
},
"permissions" : [ "idle" ],
"browser_action" : {
"default_icon" : "sample-19.png"
},
"icons" : {
"16" : "sample-16.png",
"48" : "sample-48.png",
"128" : "sample-128.png"
},
"manifest_version": 2
}
background.js:
var history_log = [];
chrome.idle.onStateChanged.addListener(function(newstate) {
var time = new Date();
if (history_log.length >= 20) {
history_log.pop();
}
history_log.unshift({'state':newstate, 'time':time});
});
chrome.browserAction.onClicked.addListener(function() {
window.open('history.html', 'testwindow', 'width=700,height=600');
});
FAIL: in my code to get the chrome.idle.queryState, http://localhost/index.html
:
<html>
<script>
document.addEventListener('DOMContentLoaded', function() {
chrome.idle.queryState(5, function(state) {
var time = new Date();
alert("extension: " + state);
});
});
</script>
</html>
EDIT:
it only works when i use as chrome-extension:// but does not work if i try to use it from http:// or https://. My goal is to make it work from http:// or https:// (or iFrame opening chrome-extension invisibly if possible?)
Having an extension open a website does not magically grant the site rights to use Chrome Extension API.
Simplest is to have
index.html
as part of the extension's files. Then you can use the API, but take note of the Chrome's CSP (you can't use inline scripts).If your goal is specifically to provide a web page access to the Chrome API, the webpage will need to talk to the extension.
There are many methods for that, for example
"externally_connectable"
, or talking via a context script and shared DOM.
In any case: chrome.idle
can only be called from extension's own pages, including but not limited to the background page.
Also, please use chrome.windows.create
or chrome.tabs.create
instead of window.open
in an extension. It doesn't require special permissions.
这篇关于谷歌浏览器 - 扩展铬空闲不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!