// ==UserScript==
// @name Linkify
// @version 100.100.100
// @author 极品小猫
// namespace http://youngpup.net/userscripts
// @description Looks for things in the page that look like URLs but aren't hyperlinked, and converts them to clickable links.
// @include *://*/*
// @match *://*/*
// @exclude https://www.google.com/*
// @exclude http://note.sdo.com/*
// @require http://code.jquery.com/jquery-2.1.4.min.js
// @grant none
// @run-at document-idle
// @grant GM_addStyle
// @grant GM_registerMenuCommand
// @grant unsafeWindow
// @namespace https://greasyfork.dpdns.org/users/3128
// ==/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;}');
//const urlRegex = /\b(https?:\/\/[^\s+\"\'\<\>,]+|ed2k:\/\/\|file\|[^\|]+\|\d{9}\|\w{32}\|h=\w{32}\|\/)/ig;
//const urlRegex = /\b(https?:\/\/[^\s+\"\\'<\>,]+|magnet:\?xt=urn:btih:\w{40}|ed2k:\/\/\|file\|[^\|]+\|\d+\|\w{32}\|(?:h=\w{32}\|)?\/)/ig;
var urlRegex=new RegExp(//协议规则
'\\b('
+
'https?:\\/\\/[^\\s+"\'<>,\\[\\]()]+'
+'|'+
'https?:\\/\\/mega.nz\\/#!\w+![^\\s+"\'<>,\\]]+'
+'|'+
'ed2k:\\/\\/\\|file\\|[^\\|]+\\|\\d+\\|\\w{32}\\|(?:h=\\w{32}\\|)?\\/'
+'|'+
'magnet:\\?xt=urn:btih:\\w{40}(&[\\w\\s]+)?'
+')','ig');
//console.log(urlRegex);
//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", "p", "q", "samp", "small", "span", "strike", "font",
"s", "strong", "sub", "sup", "td", "th", "tt", "u", "var",
"h1", "h2", "h3", "h4", "h5", "h6",
'url','img'
];
/*
var xpath = "//text()[(parent::" + allowedParents.join(" or parent::") + ") and " +
"contains(translate(., 'HTTP', 'http'), 'http')]";
*/
var xpath = "//text()[(parent::" + allowedParents.join(" or parent::") + ") and " +
"(contains(translate(., 'HTTP', 'http'), 'http')"+" or " +
"contains(translate(., 'MAGNET', 'magnet'), 'magnet')"+" or " +
"contains(translate(., 'ED2K', 'ed2k'), 'ed2k'))]";
//console.log(xpath);
var candidates = document.evaluate(xpath, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
var t0 = new Date().getTime();
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)(?:\?.+?)?$/.test(match[0])){//如果是图片链接,则创建有图片地址的连接
span.innerHTML='<img src="'+match[0]+'" class="newhref"/><br>';
}
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();
}
}
var t1 = new Date().getTime();
//alert((t1 - t0) / 1000);
}
function ViewIMG(){
var oA=document.getElementsByTagName('a');
for(var i in oA){
console.log(oA[i].href);
if(/\.(?:jpg|png|gif)$/i.test(oA[i].href)){
oA[i].innerHTML+='<img src="'+oA[i].href+'">';
}
}
}
function ViewVideo(){
$('a[href*=".mp4"]').each(function(){
$('<video controls preload>').attr({'src':this.href}).insertAfter(this);
})
}
var Mother={
historyUrl:function(){
window.history.pushState('state','', urls.replace(location.search,''));
},
}
var Host=location.hostname.replace(/^www\./i,'').toLowerCase();
var paths=location.pathname;
var search=location.search;
var HostArr={
'taptap.com':true,
'10minutemail.com':true,'pinsedashi.com':true,
'zdfans.com':true
}
if(HostArr[Host]){
Linkify();
} else if(/(\/thread-\d+-)/i.test(paths)||/mod=viewthread/i.test(search)) {
//DZ论坛监听
Linkify();
window.addEventListener('load',function(){
addMutationObserver('#postlist',Linkify);
});
}
GM_registerMenuCommand('Linkify', Linkify);
GM_registerMenuCommand('ViewIMG', ViewIMG);
GM_registerMenuCommand('ViewVideo', ViewVideo);
GM_registerMenuCommand('移除location.Search信息', Mother.historyUrl);
function addMutationObserver(selector, callback, Kill) {
var watch = document.querySelector(selector);
if (!watch) return;
var observer = new MutationObserver(function(mutations){
var nodeAdded = mutations.some(function(x){ return x.addedNodes.length > 0;});
if (nodeAdded) {
callback(mutations);
}
});
observer.observe(watch, {childList: true, subtree: true});
}