【问题标题】:Google chrome api javascript detect file open in browserGoogle chrome api javascript检测在浏览器中打开的文件
【发布时间】: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


【解决方案1】:

非常感谢 wOxxOm 给我一个提示。我目前拥有的权限是“:///”。我需要在权限中包含“file://*”。我认为 *: 应该足够好,但在这种情况下不是。所以现在我可以在 chrome.webRequest.onBeforeRequest 中看到 file:// 协议

【讨论】:

  • 我尝试了建议的技术,但没有成功。我看到页面执行的很多请求 - .js 和 .css 文件,但不是 .html 本身,不是第一个请求。请您编写代码在通过浏览器打开本地文件时拦截对本地文件的请求?
  • 我使用的是 Firefox,结果发现当打开本地文件时 Firefox 不会触发该事件,但 Opera(可能还有 Chrome)会触发。顺便说一句,Firefox 在打开本地文件时会触发 onBeforeNavigate 事件,而 Opera 不会。
猜你喜欢
  • 1970-01-01
  • 2015-09-08
  • 2022-06-13
  • 2022-08-04
  • 1970-01-01
  • 2020-02-06
  • 1970-01-01
  • 1970-01-01
  • 2021-09-29
相关资源
最近更新 更多