我使用 match 来限制我的脚本只在一个域中运行,但 chrome 在每个域中运行它。我尝试了 @include
和 @match
,当我尝试安装它并在所有网站上运行时,它显示“访问所有网站上的数据”。
如何在 chrome 中将用户脚本限制为一个域?
元数据与此页面相同:http://www.chromium.org/developers/design-documents/user-scripts
我的意思是:
// @match http://*.google.com/*
// @match http://www.google.com/*
最佳答案
Note: this answer developed between the OP and Rob W. Placing it here in the hopes that this question might be useful to others without having to sift through the comment chain, above.
有两个问题。一、a userscript header does not parse if a UTF8 BOM is present ( Chrome 错误 102667)。
其次,当在用户脚本中使用 @include
与 @match
时,Chrome 误导性地报告该脚本可以“访问您在所有网站上的数据”,但事实并非如此真的。该脚本将仅在包含语句指定的那些站点上运行。
考虑(或制作)这三个脚本:
UTF 测试,而不是 UTF.user.js(使用 ANSI 编码保存):
// ==UserScript==
// @name Not UTF source file
// @match http://www.yahoo.com/*
// ==/UserScript==
if (location.hostname != 'www.yahoo.com')
alert ("This script should not run on "+location.hostname+"!");
UTF测试,是UTF.user.js(用UTF-8编码保存,包括BOM):
// ==UserScript==
// @name Is UTF source file
// @match http://www.yahoo.com/*
// ==/UserScript==
if (location.hostname != 'www.yahoo.com')
alert ("This script should not run on "+location.hostname+"!");
include, not match.user.js(用ANSI编码保存):
// ==UserScript==
// @name Use include, not match
// @include http://www.yahoo.com/*
// ==/UserScript==
if (location.hostname != 'www.yahoo.com')
alert ("This script should not run on "+location.hostname+"!");
请注意,所有 3 个脚本都是相同的代码。只有 @name
和/或文件格式和/或 @include
与 @match
不同。
匹配的 ANSI 脚本(UTF 测试,而不是 UTF.user.js)报告这些权限:
此脚本按预期正确运行和报告。
UTF-8 脚本,匹配(UTF 测试,是 UTF.user.js)报告这些权限:
权限报告不正确,与 @match
语句相矛盾。另请注意,文件名显示为 URL 编码,而不是 @name
指令。这些都是一些不对劲的线索。
更糟的是,此脚本将在所有站点上运行。也就是说,您将在所有非 Yahoo 页面上看到 alert()
。这显然是a bug .
带有 include(Include,而不是 match.user.js)的 ANSI 脚本报告了这些权限:
虽然这是一份误导性报告,但该脚本实际上会正确运行。也就是说,它只会为雅虎页面触发。
部分原因在于 Chrome 将用户脚本自动转换为扩展程序的方式。 @match
语句直接转换为 manifest.json
的 matches
属性,而 @include
语句进入 include_globs
值。参见 Match patterns and globs .
权限报告关闭 matches
数组。
关于javascript - 尽管设置了@match 和@include 设置,Chrome 用户脚本仍会在所有页面上触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16403175/