不要运行 500 多个.attr()
语句!使用更有效的方式处理页面。
像这样创建两个文件:
old_URLs.js:
var oldUrlArray = [
"Old address 1",
"Old address 2",
"Old address 3",
// etc., etc.
]
new_URLs.js:
var newUrlArray = [
"New address 1",
"New address 2",
"New address 3",
// etc., etc.
]
并将它们放在与您的 gm.user.js
文件相同的文件夹中。
然后你的脚本变成:
// ==UserScript==
// @name _Mass link replacer remapper
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require old_URLs.js
// @require new_URLs.js
// @grant GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
introduced in GM 1.0. It restores the sandbox.
*/
var oldLinks = $('a[href*="pattern-of-old-cms"]');
while (oldLinks.length) {
var firstHref = oldLinks[0].href;
var hrefIndex = oldUrlArray.indexOf (firstHref);
if (hrefIndex >= 0) {
var toReplace = oldLinks.filter ("[href='" + firstHref + "']");
toReplace.attr ("href", newUrlArray[hrefIndex]);
oldLinks = oldLinks.not (toReplace);
}
else {
alert ("I don't know how to map the link: " + firstHref);
break;
}
}