// ==UserScript==
// @name Linkify·改
// @version 1.1.5.1
// @author 极品小猫
// @description 文本转超链接
// @en:description Looks for things in the page that look like URLs but aren't hyperlinked, and converts them to clickable links.
// @include *://*/*
// @exclude http*://www.baidu.com/*
// @exclude http*://www.google.com/*
// @exclude http*://note.sdo.com/*
// @exclude https://pan.baidu.com/*
// @require https://cdn.staticfile.org/jquery/2.1.4/jquery.min.js
// @run-at document-idle
// @grant GM_addStyle
// @grant GM_notification
// @grant GM_registerMenuCommand
// @grant unsafeWindow
// @namespace https://greasyfork.org/users/3128
// @license GPL-3.0
// @downloadURL https://update.greasyfork.icu/scripts/369895/Linkify%C2%B7%E6%94%B9.user.js
// @updateURL https://update.greasyfork.icu/scripts/369895/Linkify%C2%B7%E6%94%B9.meta.js
// ==/UserScript==
function Linkify() {
GM_addStyle('.newhref{border:2px dashed red!important;display:inline-block!important;padding:5px!important;margin:5px 0!important;}.newhref:hover{border-color:#E9aa2f!important;}');
let matchRule = `[^\\s+"\\\'<>,\\[\\]()\:\\*\\u4E00-\\u9FFF]`,
urlRegexRule=[ //协议规则,正则模式,\需要转义\\
`https?:\\/\\/[^\\/]+/?(${matchRule}+|$)`, //ALL,http通用规则
'(?:https?:\\/\\/)?\\w+(%E7%82%B9|点)\\w+(%E7%82%B9|点)\\w+\\/[^\\s+"\\\'<>,\\[\\]()\:\\*\\u4E00-\\u9FFF]+', //域名中包含中文 . → 点
'https?:\\/\\/mega.nz\\/#!\w+![^\\s+"\'<>,\\]]+', //mega 专用规则
'ed2k:\\/\\/\\|file\\|[^\\|]+\\|\\d+\\|\\w{32}\\|(?:h=\\w{32}\\|)?\\/', //ed2k专用规则
'magnet:\\?xt=urn:btih:\\w{40}(&[\\w\\s]+)?' //magnet 专用规则
], urlRegexStr=urlRegexRule.join('|');
var urlRegex=new RegExp(`\\b(${urlRegexStr})`,'ig'); //协议规则,正则模式,\需要转义\\
//ed2k://|file|mura-041616_381.mp4|1874185231|E7D60A7A854CF7AE0FF77AC306808760|h=SYGOH7KKTWHJNA4OBVAEOANW27FT5RJE|/
// tags we will scan looking for un-hyperlinked urls
var allowedParents = [ //允许处理的文本的父标签
"body",
"code", "blockquote",
"abbr", "acronym", "address", "applet", "b", "bdo", "big",
"caption", "center", "cite", "dd", "del", "div", "dfn", "dt", "em",
"fieldset", "font", "form", "i", "iframe",
"ins", "kdb", "li", "object", "pre[not(contains(@class, 'brush'))]", "p", "q", "samp", "small", "span", "strike", "font",
"s", "strong", "sub", "sup", "td", "th", "tt", "u", "var",
"h1", "h2", "h3", "h4", "h5", "h6",
'url','img'
];
//[not(contains(@class, "brush"))],避免与 SyntaxHighlighter 高亮插件冲突
let allowedParentsRule = allowedParents.join(" or parent::");
let xpath = `//text()[(parent::${allowedParentsRule}) and (contains(translate(., 'HTTP', 'http'), 'http') or contains(translate(., 'MAGNET', 'magnet'), 'magnet') or contains(translate(., 'ED2K', 'ed2k'), 'ed2k'))]`;
var candidates = document.evaluate(xpath, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for (var cand = null, i = 0; (cand = candidates.snapshotItem(i)); i++) {
if (urlRegex.test(cand.nodeValue)) {
var span = document.createElement("span"); //为超链接创建span标签作为容器
var source = cand.nodeValue;
//console.log(cand);
cand.parentNode.replaceChild(span, cand);
urlRegex.lastIndex = 0;
for (var match = null, lastLastIndex = 0; (match = urlRegex.exec(source)); )
{
span.appendChild(document.createTextNode(source.substring(lastLastIndex, match.index)));
//console.log(match);
if(/\.(?:gif|jpg|png|bmp|webp)(?:\?.+?)?$/.test(match[0])){//如果是图片链接,则创建有图片地址的连接
span.innerHTML='
';
}
var Mtxt=match[0]; //取得链接
var Rtxt=/paco-\d{6}_\d{3}/i.test(Mtxt) ? Mtxt.replace(/paco-(\d{6}_\d{3})/i,'pacopacomama $1') : Mtxt; //替换链接内容
var a = document.createElement("a");
a.href=Rtxt;
a.className='newhref';
a.target="blank";
a.appendChild(document.createTextNode(decodeURIComponent(Rtxt)));
span.appendChild(a);
lastLastIndex = urlRegex.lastIndex;
}
span.appendChild(document.createTextNode(source.substring(lastLastIndex)));
span.normalize();
}
}
}
function ViewIMG(){
var oA=document.getElementsByTagName('a');
for(var i in oA){
//console.log(oA[i].href);
if(!oA.selector('img')) {
if(/\.(?:jpg|png|gif)$/i.test(oA[i].href)){
oA[i].innerHTML+='
';
}
}
}
}
function ViewVideo(){
$('a[href*=".mp4"], a[href*=".m3u8"]').each(function(){
$('