【问题标题】:Including a Greasemonkey script across multiple domains包括跨多个域的 Greasemonkey 脚本
【发布时间】:2013-06-14 15:20:22
【问题描述】:

这是一个有点奇怪的具体问题。

我正在编写一个将跨十个域运行的 Greasemonkey 脚本。这些网站都有相同的结构,但每个网站的域名不同。例如,脚本将运行在:

http://first-domain.com/
http://another-one.com/
http://you-get-the-point.com/

我还需要它在同一域的其他页面上运行,因此这些域中的一个的列表将类似于:

http://first-domain.com/admin/edit/*
http://first-domain.com/blog/*
http://first-domain.com/user/*/history

显然,如果我为所有十个域都包含这三个路径,那么我需要将 30 个 URL 列为 @includes。

所以我想知道是否有办法做类似的事情:

// Obviously fake code:

var list_of_sites = ["first-domain", "another-one", "you-get-the-point"];

@include http:// + list_of_sites[any] + .com/admin/edit/*
@include http:// + list_of_sites[any] + .com/blog/*
@include http:// + list_of_sites[any] + .com/user/*/history

如果可能发生这样的事情,它会将@includes 的列表从 30 个减少到 3 个。

这可能吗,还是我在做梦?

PS 我知道我可以只使用@include http://first-domain.com/* 然后使用if 语句在该域中的某些路径上运行脚本的某些部分,但脚本的预期页数运行只占网站的 2% 左右,因此在每个网站的每个页面上都包含脚本似乎很浪费。

【问题讨论】:

    标签: greasemonkey userscripts


    【解决方案1】:

    参考:

    适用于 Greasemonkey(即 Firefox)的解决方案在 Chrome 和 Tampermonkey 上可能有所不同。

    三种基本方法:

    1. 使用 30 条不同的 @include:虽然这在剪切和粘贴编码方面可能令人不快,但它是一种在浏览器中工作相同的方法,并且拥有最佳的浏览器性能。其他方法要求浏览器对可能访问的每个页面或 iframe 进行(更多)检查。

    2. 使用正则表达式@include

      @include /^http:\/\/(1stDomain\.com|2ndDomain\.com|3rdDomain\.net|etc.)\/(admin\/edit|blog|user\/.+?\/history)/
      

    这是一条线,性能相当不错,但这条线可能会变得笨拙,并且仅适用于 Greasemonkey 和 Tampermonkey(可能是 Scriptish)。

    1. 使用@match@include@exclude 的各种组合:我只提到这是一种可能性。这是直接 Chrome 上性能最好的方法,但对于这种事情来说不是很跨浏览器。对于 Greasemonkey 或 Tampermonkey,请使用方法 1 或方法 2。

    我建议您尽可能避免使用前导通配符。它们使浏览器的速度减慢最多。 EG,如果可以避免的话,不要使用@include /^.+ .../@include http:/*/* 之类的东西。

    【讨论】:

    • 漂亮,这太棒了!方法#2 最适合我的情况,而且我现在已经让它完美地工作了。它适用于仅针对 Greasemonkey 和 Tampermonkey 的私有脚本,因此兼容性很好。你是个传奇!
    猜你喜欢
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 2012-04-28
    • 2011-03-06
    • 2012-08-18
    相关资源
    最近更新 更多