Javascript 探测扩展时出现 GET net::ERR_FAILED 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37514297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GET net::ERR_FAILED error when probing for extension
提问by sidanmor
There is a certain Chrome extension and I want to get a PNG file from it by XMLHttpRequest
. If the extension is enabled, I want to write 'load' to the console, and if the extension is disabled, I want to write 'error' to the console.
有一个特定的 Chrome 扩展程序,我想通过XMLHttpRequest
. 如果启用了扩展,我想向控制台写入'load',如果禁用扩展,我想向控制台写入'error'。
It works fine, but if the Extension is disabled, Chrome writes an error in the console that I do not want to appear:
它工作正常,但如果扩展程序被禁用,Chrome 会在控制台中写入一个我不想出现的错误:
How can I remove this error from the console?
如何从控制台中删除此错误?
(I have tried window.onerror
but it doesn't work)
(我试过了,window.onerror
但没有用)
The code:
编码:
var loadHref = function(href) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function(){console.log('load')};
xmlhttp.onerror = function() {console.log('error');};
xmlhttp.open('GET', href);
xmlhttp.send();
}
loadHref('chrome-extension://77672b238520494cba8855547dd00ba8/img/icon24.png');
采纳答案by Xan
Basically, you can't silence those errors, as they are not JS errors but network errors.
基本上,您无法消除这些错误,因为它们不是 JS 错误而是网络错误。
Assuming your goal is to detect that a specific extension is present:
假设您的目标是检测是否存在特定扩展名:
Assume you need it at a specific domain and for a specific extension that is controlled by you.
In this case, the optimal approach is
externally_connectable
communication. Here's a sample.Assume you need it at a non-specific domain not known in advance, but you control the extension.
In this case, a Content Scriptcan be injected (probably with
"run_at": "document_start"
) and add something to the document signalling the presence of the extension. For example, injecting a page-level scriptthat sets a variable.Assume you don't control the extension.
Well, in that case you're screwed. If an extension won't cooperate in the manners described above, probing its web-accessible resources (if any!) is the only way to detect it, short of watching for specific content script activity in the DOM (again, if any).
假设您在特定域和由您控制的特定扩展需要它。
在这种情况下,最佳方法是
externally_connectable
通信。这是一个示例。假设您在事先未知的非特定域中需要它,但您控制扩展。
在这种情况下,可以注入内容脚本(可能使用
"run_at": "document_start"
)并向文档添加一些内容,以表明扩展的存在。例如,注入设置变量的页面级脚本。假设您不控制扩展名。
那么,在那种情况下你就完蛋了。如果扩展程序不会以上述方式合作,则探测其 Web 可访问资源(如果有的话!)是检测它的唯一方法,而不是观察 DOM 中的特定内容脚本活动(再次,如果有的话)。
回答by Teyam
Actually, there is already an existing issue on error when chrome cast extension is not installedwith google-cast-sdk and based on that issues tracker, this hasn't been totally resolved yet. There are, however, given workarounds from one of the comments:
实际上,当 chrome cast 扩展没有与 google-cast-sdk 一起安装时,已经存在一个错误问题,并且基于该问题跟踪器,这还没有完全解决。但是,其中一条评论给出了解决方法:
the workarounds would be to either install the Google Cast extension or disable network warnings (please note you may miss some warnings that could be on interest to you) so you don't see these additional logs.
解决方法是安装 Google Cast 扩展程序或禁用网络警告(请注意,您可能会错过一些您可能感兴趣的警告),这样您就不会看到这些额外的日志。
And, you may also try with the probable solutions given in this SO post - Google chrome cast sender error if chrome cast extension is not installed or using incognitoand who knows, it might help. :)
而且,您也可以尝试使用此 SO 帖子中给出的可能解决方案 -如果未安装 chrome cast 扩展程序或使用隐身模式,则Google chrome cast sender 错误,谁知道,这可能会有所帮助。:)