如何检测浏览器上的扩展名?
How to detect extension on a browser?
我正在尝试检测用户浏览器上是否安装了扩展程序。
我试过这个:
var detect = function(base, if_installed, if_not_installed) {
var s = document.createElement('script');
s.onerror = if_not_installed;
s.onload = if_installed;
document.body.appendChild(s);
s.src = base + '/manifest.json';
}
detect('chrome-extension://' + addon_id_youre_after, function() {alert('boom!');});
如果浏览器安装了扩展程序,我会收到如下错误:
Resources must be listed in the web_accessible_resources manifest key
in order to be loaded by pages outside the extension
GET chrome-extension://invalid net::ERR_FAILED
如果没有,我会得到一个不同的错误。
GET chrome-extension://addon_id_youre_after/manifest.json net::ERR_FAILED
这是我遇到的错误的图片:
我试图捕捉错误 (fiddle)
try {
var s = document.createElement('script');
//s.onerror = window.setTimeout(function() {throw new Error()}, 0);
s.onload = function(){alert("installed")};
document.body.appendChild(s);
s.src = 'chrome-extension://gcbommkclmclpchllfjekcdonpmejbdp/manifest.json';
} catch (e) {
debugger;
alert(e);
}
window.onerror = function (errorMsg, url, lineNumber, column, errorObj) {
alert('Error: ' + errorMsg + ' Script: ' + url + ' Line: ' + lineNumber
+ ' Column: ' + column + ' StackTrace: ' + errorObj);
}
到目前为止,我无法捕捉到错误..
任何帮助将不胜感激
让您的 Chrome 扩展程序在您的页面上查找具有非常特定 ID 的特定 DIV 或其他元素。
例如:-
<div id="ExtensionCheck_JamesEggersAwesomeExtension"></div>
第一个错误是 Chrome 提供的信息,直接注入控制台,您无法捕获(如您所见)。
GET
错误来自网络堆栈。 Chrome 在任何一种情况下都拒绝加载并模拟网络错误 - 您可以 catch with onerror
handler on the element itself,但不能在 window.onerror
处理程序中。引用,强调我的:
When a resource (such as an <img>
or <script>
) fails to load, an error event using interface Event
is fired at the element, that initiated the load, and the onerror()
handler on the element is invoked. These error events do not bubble up to window, but (at least in Firefox) can be handled with a single capturing window.addEventListener
.
这是一个至少可以检测网络错误的示例。请再次注意,您不能 catch 它们,因为这会阻止它在控制台中显示。当 Google Cast 扩展(公开资源)将其用作检测方法时,它是 source of an embarrasing problem。
s.onload = function(){alert("installed")};
s.error = function(){alert("I still don't know")};
请注意,您无法区分两者。在内部,Chrome 将其中一个请求重定向到 chrome-extension://invalid
,但此类重定向对您的代码是透明的:无论是加载资源(就像您所做的那样)还是使用 XHR。即使是新的 Fetch API,它应该给予对重定向的更多控制,但也无济于事,因为它不是 HTTP 重定向。它得到的只是一个无信息的网络错误。
因此,您无法检测扩展是否未安装或已安装,但不会暴露资源。
请理解这是有意为之。您提到的方法曾经有效 - 您可以获取已知名称的任何资源。但这是指纹浏览器的一种方法 - Google 明确调用 "malicious" 并想要阻止的东西。
因此,web_accessible_resources
模型在 Chrome 18 年(一直追溯到 2012 年 8 月)引入,以防止扩展被嗅探 - 需要 explicitly declare resources 被暴露。引用,强调我的:
Prior to manifest version 2 all resources within an extension could be accessed from any page on the web. This allowed a malicious website to fingerprint the extensions that a user has installed or exploit vulnerabilities (for example XSS bugs) within installed extensions. Limiting availability to only resources which are explicitly intended to be web accessible serves to both minimize the available attack surface and protect the privacy of users.
Google 积极打击指纹识别,只有合作的扩展才能可靠地检测到。 可能存在特定于扩展程序的黑客攻击 - 例如特定的 DOM 更改、请求拦截或您可以获取的公开资源 - 但没有通用方法,并且扩展程序可能会更改其 "visible signature"任何时候。 我在这个问题中解释过:,但我希望你能更好地理解它的原因。
综上所述,如果您确实找到了一种公开任意扩展指纹识别的通用方法,这将被视为恶意和 Chrome 中的隐私漏洞。
我正在尝试检测用户浏览器上是否安装了扩展程序。
我试过这个:
var detect = function(base, if_installed, if_not_installed) {
var s = document.createElement('script');
s.onerror = if_not_installed;
s.onload = if_installed;
document.body.appendChild(s);
s.src = base + '/manifest.json';
}
detect('chrome-extension://' + addon_id_youre_after, function() {alert('boom!');});
如果浏览器安装了扩展程序,我会收到如下错误:
Resources must be listed in the web_accessible_resources manifest key in order to be loaded by pages outside the extension
GET chrome-extension://invalid net::ERR_FAILED
如果没有,我会得到一个不同的错误。
GET chrome-extension://addon_id_youre_after/manifest.json net::ERR_FAILED
这是我遇到的错误的图片:
我试图捕捉错误 (fiddle)
try {
var s = document.createElement('script');
//s.onerror = window.setTimeout(function() {throw new Error()}, 0);
s.onload = function(){alert("installed")};
document.body.appendChild(s);
s.src = 'chrome-extension://gcbommkclmclpchllfjekcdonpmejbdp/manifest.json';
} catch (e) {
debugger;
alert(e);
}
window.onerror = function (errorMsg, url, lineNumber, column, errorObj) {
alert('Error: ' + errorMsg + ' Script: ' + url + ' Line: ' + lineNumber
+ ' Column: ' + column + ' StackTrace: ' + errorObj);
}
到目前为止,我无法捕捉到错误..
任何帮助将不胜感激
让您的 Chrome 扩展程序在您的页面上查找具有非常特定 ID 的特定 DIV 或其他元素。
例如:-
<div id="ExtensionCheck_JamesEggersAwesomeExtension"></div>
第一个错误是 Chrome 提供的信息,直接注入控制台,您无法捕获(如您所见)。
GET
错误来自网络堆栈。 Chrome 在任何一种情况下都拒绝加载并模拟网络错误 - 您可以 catch with onerror
handler on the element itself,但不能在 window.onerror
处理程序中。引用,强调我的:
When a resource (such as an
<img>
or<script>
) fails to load, an error event using interfaceEvent
is fired at the element, that initiated the load, and theonerror()
handler on the element is invoked. These error events do not bubble up to window, but (at least in Firefox) can be handled with a single capturingwindow.addEventListener
.
这是一个至少可以检测网络错误的示例。请再次注意,您不能 catch 它们,因为这会阻止它在控制台中显示。当 Google Cast 扩展(公开资源)将其用作检测方法时,它是 source of an embarrasing problem。
s.onload = function(){alert("installed")};
s.error = function(){alert("I still don't know")};
请注意,您无法区分两者。在内部,Chrome 将其中一个请求重定向到 chrome-extension://invalid
,但此类重定向对您的代码是透明的:无论是加载资源(就像您所做的那样)还是使用 XHR。即使是新的 Fetch API,它应该给予对重定向的更多控制,但也无济于事,因为它不是 HTTP 重定向。它得到的只是一个无信息的网络错误。
因此,您无法检测扩展是否未安装或已安装,但不会暴露资源。
请理解这是有意为之。您提到的方法曾经有效 - 您可以获取已知名称的任何资源。但这是指纹浏览器的一种方法 - Google 明确调用 "malicious" 并想要阻止的东西。
因此,web_accessible_resources
模型在 Chrome 18 年(一直追溯到 2012 年 8 月)引入,以防止扩展被嗅探 - 需要 explicitly declare resources 被暴露。引用,强调我的:
Prior to manifest version 2 all resources within an extension could be accessed from any page on the web. This allowed a malicious website to fingerprint the extensions that a user has installed or exploit vulnerabilities (for example XSS bugs) within installed extensions. Limiting availability to only resources which are explicitly intended to be web accessible serves to both minimize the available attack surface and protect the privacy of users.
Google 积极打击指纹识别,只有合作的扩展才能可靠地检测到。 可能存在特定于扩展程序的黑客攻击 - 例如特定的 DOM 更改、请求拦截或您可以获取的公开资源 - 但没有通用方法,并且扩展程序可能会更改其 "visible signature"任何时候。 我在这个问题中解释过:
综上所述,如果您确实找到了一种公开任意扩展指纹识别的通用方法,这将被视为恶意和 Chrome 中的隐私漏洞。