我有以下脚本可以自动跟随Google图像搜索结果中的IFRAME:
// ==UserScript==
// @name Follow IFRAME in Google image search
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match http://images.google.de/imgres?*
// @grant none
// ==/UserScript==
/* jshint -W097 */
'use strict';
location.replace(document.querySelector('iframe').src)
效果很好。
现在我想让它独立于德国顶级域名。但是当我将匹配更改为
// @match http://images.google.*/imgres?*
它停止工作。 Chrome 的 documentation 的
@match
选项说道:
┌──────────────────────┬──────────────────────────── ──────────────────────────────┐ │ 不好的模式 │ 为什么不好 │ ├──────────────────────┼────────────────────────── ──────────────────────────────┤ │ http://foo.*.bar/baz │ 如果'*'在主机中,它必须是第一个字符 │ └────────────────────────┴──────────────────────────── ──────────────────────────────┘
如何匹配任何具有此类限制的顶级域名?
在 Tampermonkey 和 Greasemonkey 中,
@include
指令允许您使用 .tld
作为神奇的通配符。
// WORKS IN TAMPERMONKEY AND GREASEMONKEY
//
// @include https://images.google.tld/*
其中
.tld
将匹配任何标准化顶级域名。
请注意,此 不适用于 @match
,并且可能不适用于所有用户脚本扩展。
// DOES NOT WORK
//
// @match https://images.google.tld/*
奇怪的是,它不在 TamperMonkey 的 文档(2023 年 10 月)中。也许是因为它的兼容性不广泛,但使用起来却很诱人。
此外,如果你使用@include
,ESLint 会抱怨。我直接无视了。
// DO NOT WORK
//
// @match https://images.google.tld/*
// @match https://images.google.*.*/*
虽然这些确实有效,但它们显然很糟糕:
// WORKS, BUT HORRIBLE and possibly deprecated
//
// @include /https:\/\/images\.google\.[a-z]{2,3}(\.[a-z]{2})?\/.*/
// OR
// WIDELY COMPATIBLE, BUT HORRIBLE
//
// @match https://images.google.com/*
// @match https://images.google.de/*
// @match https://images.google.fr/*
// @match https://images.google.co.uk/*
// @match https://images.google.com.my/*
// ... and 67 more lines ...