【发布时间】:2018-10-29 07:54:21
【问题描述】:
对于 google chrome 扩展,我需要能够检测用户是否使用 chrome 浏览器打开 .html 文件。
我尝试了不同的方法,但似乎我没有收到事件
"permissions": ["webRequest","browsingData", "fileSystemProvider"],
"file_system_provider_capabilities": {
"configurable": true,
"watchable": true,
"source": "file"
},
我试图在 webRequest 中拦截
chrome.webRequest.onBeforeSendHeaders.addListener(function(info){
// info.url does not give me the file.html which opened
)};
我尝试了不同的文件事件处理方法,但都没有触发
chrome.fileSystemProvider.onOpenFileRequested.addListener(function(file){
console.log("open file");
});
chrome.fileSystemProvider.onReadFileRequested.addListener(function(file){
console.log("read file");
});
chrome.fileSystemProvider.onExecuteActionRequested.addListener(function(){
console.log("execute file");
});
提前感谢您的帮助
【问题讨论】:
-
使用 chrome.tabs.onUpdated 监听器。 webRequest 显然是针对 web 请求的,而 fileSystemProvider 是提供虚拟文件系统的。
-
我确实收到了 tabs.onUpdated 的事件,但我看不到文件名。这个想法是当用户从本地打开 html 文件时,我应该能够检测和扫描内容。
-
选项卡 URL 是文件名,因此如果您在 manifest.json(或
file://*)中有"permissions": ["<all_urls>"],则应该没有问题。文档应该有示例。 -
非常感谢您的评论,它确实给了我一些提示。我现在获得的许可是 "://*/" 但是还不够,我还需要包含 "file://" 。所以现在我可以在 chrome.webRequest.onBeforeRequest 中捕获文件协议了。
标签: javascript google-chrome google-chrome-extension google-chrome-app chrome-app-developer-tool