< User:Zorua Fox | JWB.js
![刷新 刷新](/images/thumb/b/b0/OOjs_UI_icon_reload-progressive.svg/55px-OOjs_UI_icon_reload-progressive.svg.png)
请注意:更新本页面后,您可能需要清除浏览器缓存才能看到所作变更的影响。
Google Chrome、Microsoft Edge、Mozilla Firefox、Safari:按住⇧ Shift,同时单击“刷新”。
Google Chrome、Microsoft Edge、Mozilla Firefox、Safari:按住⇧ Shift,同时单击“刷新”。
警告:如果您是根据讨论页、电子邮件或其他通信工具的留言要求来这里操作的,请务必保持警惕。相关留言要求您添加到本页面的内容里可能包含了盗取用户账号的恶意功能,如果您将其插入了您的自定义界面设置(即本页面),可能为您带来严重的风险!
如果您无法确定相关操作是否安全,请前往有兽茶谈询问相关事宜。
如果您无法确定相关操作是否安全,请前往有兽茶谈询问相关事宜。
警告:用户可能引用来自其他网站的脚本或样式,相关内容可能不以CC BY-SA 4.0许可;使用或改编此页面内容前,请确认相关内容的著作权许可协议!
/**
* Web Worker for AutoWikiBrowser script. Used to run certain tasks on a parallel process to be able to terminate it.
* Idea from https://stackoverflow.com/a/66162733/1256925. Dirty hack workaround due to https://stackoverflow.com/q/66188950/1256925.
* See https://en.wikipedia.org/wiki/User:Joeytje50/JWB.js for the full script, as well as licensing.
* Licensed under GNU GPL 2. http://www.gnu.org/copyleft/gpl.html
*/
JWB.worker.function = function() {
var ret = {};
function regexMatch(str, pattern, flags, callback) {
var re, result;
try {
re = new RegExp(pattern, flags);
result = str.match(re);
} catch(e) {
// catch and propagate invalid regexes etc.
callback(undefined, e);
}
callback(result, undefined);
}
ret.match = regexMatch;
function regexReplace(str, pattern, flags, rWith, callback) {
var re, result;
try {
re = new RegExp(pattern, flags);
result = str.replace(re, rWith);
} catch(e) {
// catch and propagate invalid regexes etc.
callback(undefined, e);
}
callback(result, undefined);
}
ret.replace = regexReplace;
//function to *only* replace the parsed wikitext
//It excludes comments (<!-- -->), and nowiki, math, source, syntaxhighlight, pre, code, gallery and timeline tags)
//Based on http://stackoverflow.com/a/23589204/1256925
function unparsedReplace(str, pattern, flags, rWith, callback) {
var exclude = '(<!--[\\s\\S]*?-->|<(nowiki|math|source|syntaxhighlight|pre|gallery|timeline)[^>]*?>[\\s\\S]*?<\\/\\2>)';
//add /i flag, to exclude the correct tags regardless of casing.
//This won't matter for the actual replacing, as the specified flags are used there.
var replacements = 0;
var global = flags.indexOf('g') !== -1;
// build regex with exclusion list.
// This re will be case insensitive, but the actual replacement done will still be case sensitive if the 'i' flag is not given.
// This re will also be executed globally on the string, but the separate tracker will only allow one replacement to be done if 'g' is not given.
try {
var re = new RegExp(exclude + '|(' + pattern + ')', flags.replace(/i|$/, 'i').replace(/g|$/, 'g'));
str = str.replace(re, function(match, g1, g2, g3) {
if (!global && replacements > 0) { // a replacement has already been made in non-global mode
return match;
} else if (g3 !== undefined) { //continue to perform replacement if the match is the group that's supposed to be the match
replacements++; // keep track of the amount of replacements made.
return match.replace(new RegExp(pattern, flags), rWith);
} else { //do nothing if the match is one of the excluded groups
return match;
}
});
} catch(e) {
// catch and propagate invalid regexes etc.
callback(undefined, e);
}
callback(str, undefined);
}
ret.unparsedreplace = unparsedReplace;
onmessage = function(e) {
let data = e.data;
console.log('worker message', data);
let callback = (result, err) => postMessage({cmd: data.cmd, result: result, err: err});
switch (data.cmd) {
case 'match':
regexMatch(data.str, data.pattern, data.flags, callback);
break;
case 'replace':
regexReplace(data.str, data.pattern, data.flags, data.rWith, callback);
break;
case 'unparsedreplace':
unparsedReplace(data.str, data.pattern, data.flags, data.rWith, callback);
break;
default:
postMessage({err: 'Unknown command: ' + data.cmd});
}
};
return ret; // return object containing synchronous functions as a fallback.
};