探寻扩展名时出现GET net :: ERR_FAILED错误 [英] GET net::ERR_FAILED error when probing for extension
问题描述
有一个特定的Chrome扩展程序,我想通过XMLHttpRequest
从中获取一个PNG文件.如果启用了扩展名,我想向控制台写入加载",如果禁用了扩展名,我要向控制台写入错误".
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会在控制台中写一个我不想出现的错误:
It works fine, but if the Extension is disabled, Chrome writes an error in the console that I do not want to appear:
如何从控制台中删除此错误?
How can I remove this error from the console?
(我尝试过window.onerror
,但不起作用)
(I have tried window.onerror
but it doesn't work)
代码:
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');
推荐答案
基本上,您无法使这些错误保持沉默,因为它们不是JS错误,而是网络错误.
Basically, you can't silence those errors, as they are not JS errors but network errors.
假设您的目标是检测是否存在特定的扩展名:
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.
在这种情况下,最佳方法是 externally_connectable
通信.这是一个示例.
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.
在这种情况下,可以注入内容脚本(可能是"run_at": "document_start"
)并在文档中添加一些表示扩展名存在的信息.例如,注入页面级别脚本,用于设置变量.
In this case, a Content Script can 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 script that sets a variable.
假设您不控制扩展名.
好吧,在那种情况下,你被搞砸了.如果扩展无法以上述方式配合使用,则探测其可通过Web访问的资源(如果有的话!)是检测到它的唯一方法,而无需关注DOM中的特定内容脚本活动(同样,如果有的话).
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).
这篇关于探寻扩展名时出现GET net :: ERR_FAILED错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!