是否可以阻止 Chrome 扩展程序在特定网站上运行?
假设我有一个网站www.foo.com,我是否可以阻止 Chrome 扩展程序(特别是内容脚本)在我的网站上运行,或者阻止它们访问 DOM?
是否可以阻止 Chrome 扩展程序在特定网站上运行?
假设我有一个网站www.foo.com,我是否可以阻止 Chrome 扩展程序(特别是内容脚本)在我的网站上运行,或者阻止它们访问 DOM?
由于另一个答案并没有真正回答有关实际停止扩展的任何问题,我想我会自己加两分钱。使用另一个答案中的方法,您有时可以检测是否安装了特定的扩展并做出相应的反应,但这需要您测试特定的 ID 字符串和该特定扩展的文件。我相信我们都同意这不是一个非常全面的解决方案。
有很多关于扩展的事情是您无法从您的站点中停止的,例如chrome.webRequest
adblock 使用的 api。您无法直接干预此类代码,但您可以在 DOM 操作方面做很多事情。
Content Scripts
以isolated world
他们无法看到/与网站上运行的 javascript 交互的意义进行操作。然而,他们确实可以完全访问 DOM,并且可以为所欲为。相反,您自己的 javascript 对该 DOM 具有相同的访问权限。利用这一点,isolated world
我们可以设置一个MutationObserver
来监视 DOM 并防止任何不需要的更改。由于isolated world
,content scripts
不能禁用或关闭我们的观察者,而我们自己的 javascript 可以自由地这样做。
这是一个MutationObserver
锁定 DOM的例子,jQuery
因为我很懒。
var config= {childList: true,
attributes: true,
characterData: true,
subtree: true,
attributeOldValue: true,
characterDataOldValue: true};
var observer = new MutationObserver(function(mutations){
mutations.forEach(function(mutation){
switch(mutation.type){
case "attributes":
observer.disconnect();
if(mutation.attributeName == "class")
mutation.target.className = mutation.oldValue;
else if(mutation.attributeName=="id"||mutation.attributeName=="title")
mutation.target[mutation.attributeName] = mutation.oldValue;
else if(mutation.attributeName == "style")
mutation.target.style.cssText = mutation.oldValue;
observer.observe(document,config);
break;
case "characterData":
observer.disconnect();
mutation.target.data = mutation.oldValue;
observer.observe(document,config);
break;
case "childList":
observer.disconnect();
if(mutation.addedNodes.length > 0)
$(mutation.addedNodes[0]).remove();
if(mutation.removedNodes.length > 0){
if(mutation.nextSibling)
$(mutation.removedNodes[0]).insertBefore(mutation.nextSibling);
else if(mutation.previousSibling)
$(mutation.removedNodes[0]).insertAfter(mutation.previousSibling);
else
$(mutation.removedNodes[0]).appendTo(mutation.target);
}
observer.observe(document,config);
break;
}
});
});
$(function(){
observer.observe(document,config);
});
将其放入带有简单清单的 chrome 扩展中,例如:
{
"name": "DOM Polymerase",
"version": "1.0",
"manifest_version": 2,
"permissions": [
"tabs","<all_urls>"
],
"content_scripts": [{
"matches": ["http://example.iana.org/*"],
"js": ["jquery-1.8.3.min.js","polymerase.js"]
}]
}
并且导航到http://example.iana.org/将显示不再可能对 DOM 进行外部操作(除了一些属性,我没有将它们全部编码在那里)。当然,在这种情况下,内部操作也被拒绝,但如果代码在站点中而不是扩展中,那将是另一回事。虽然这不会完全禁用扩展,但它至少应该保留您的 DOM。
对于问题的简短回答,请转到第 4 次编辑:
您需要从要阻止的 Extension 中知道 extensionId,以便它工作。
这是来自概念 验证测试站点的测试站点
这是解决方案背后的信息: Chrome插件黑客简介:指纹识别
既然您知道正在运行的扩展程序,您可以重定向/阻止/...
我希望它有所帮助。
编辑:
在 Windows XP 上使用(Chrome 版本 27.0.1453.94)测试
编辑2:
此技术仅在以下情况下有效:
编辑3:
案例扩展:JSONView
您可以使用此代码检测扩展名(仅示例代码):
<script src="chrome-extension://chklaanhfefbnpoihckbnefhakgolnmc/error.gif" onerror="console.info('Extension Not Found')" onload="console.info('Extension Found')"></script>
<!-- since the the file error.gif is allowed in the manifest "web_accessible_resources" (any other file mentioned there would also be fine) -->
<!-- the block code should come in the onload of the script tag -->
<!-- tested with Chrome 27+ WinXp -->
一些上下文: JSONView 扩展有一个版本 2 清单:
...
"manifest_version": 2,
"name": "JSONView",
...
因此默认情况下,您无法访问上面“概念证明”中提到的清单文件。
但是它使用 Manifest 中的“web_accessible_resources”属性,该属性允许网站从扩展访问文件。
...
"web_accessible_resources": [ "jsonview.css", "jsonview-core.css", "content_error.css", "options.png", "close_icon.gif", "error.gif" ]
...
所以现在您可以从您的网页调用任何这些文件。
例子:
chrome-extension://chklaanhfefbnpoihckbnefhakgolnmc/error.gif
chrome-extension://chklaanhfefbnpoihckbnefhakgolnmc/jsonview.css
...
并且使用 Image/Script/.. -Tag 中的此 url,您可以知道扩展是否存在,如果 onload 事件触发。
Ps:我只在 Windows XP 上用 Chrome 版本 27.0.1453.94 测试了这个,在其他版本中它可能不起作用。(见 TJ Crowder 的评论)
PPs:有关更多详细信息,请查看 Chrome 开发人员资源。这是 Chrome 资源页面“指纹”东西上的扩展链接)
编辑4:
我认为它本身不能被阻止,但是如果您可以检测到上述扩展名,您可以: