Text To link

把文字链接转换为可点击链接

目前為 2014-06-16 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// Generated by IcedCoffeeScript 1.7.1-e
// ==UserScript==
// @name					Text To link
// @description				把文字链接转换为可点击链接
// @author					lkytal
// @namespace				Lkytal
// @homepage				http://coldfire.qiniudn.com/
// @include					*
// @exclude					*pan.baidu.com/*
// @exclude					*renren.com/*
// @exclude					*exhentai.org/*
// @version					2.4.4
// @icon					http://lkytal.qiniudn.com/ic.ico
// @grant					unsafeWindow
// @homepageURL				https://greasyfork.org/scripts/342-text-to-link/
// ==/UserScript==

"use strict";

if (window != window.top || window.document.title == "")
	return;
var LinkPage, excludedTags, filter, linkPack, linkify, observer, setLink, url_regexp, xpath;

url_regexp = new RegExp("(((https?://|www\\.|magnet\\:\\?)[\\x21-\\x2e\\x30-\\x7e]+|(\\w[\\x21-\\x2e\\x30-\\x7e]+\\.(com|cn|org|net|info|tv)))(/[\\x21-\\x7e]*[0-9a-zA-Z])?)", "gi");

unsafeWindow.setHttp = function(event) {
  var url;
  url = event.target.textContent;
  if (url.indexOf("http") !== 0 || url.indexOf("magnet:?") !== 0) {
    return event.target.setAttribute("href", "http://" + url);
  }
};

setLink = function(candidate) {
  var span, text;
  if ((candidate == null) || candidate.nodeName === "#cdata-section") {
    return;
  }
  text = candidate.textContent.replace(url_regexp, '<a href="$1" target="_blank" onclick="setHttp(event);">$1</a>');
  if (candidate.textContent.length === text.length) {
    return;
  }
  span = document.createElement("span");
  span.innerHTML = text;
  return candidate.parentNode.replaceChild(span, candidate);
};

excludedTags = "a,applet,input,button,area,pre,embed,frame,frameset,head,iframe,img,map,meta,noscript,object,option,param,script,select,style,textarea,code".split(",");

xpath = "//text()[not(ancestor::" + excludedTags.join(') and not(ancestor::') + ")]";

filter = new RegExp("^(textarea|input|button|pre|select|option|meta|link|noscript|a|html|head|object|embed|script|style|frameset|frame|iframe|img|code)$", "i");

linkPack = function(result, start) {
  var i, _i, _j, _ref, _ref1;
  if (start + 400 < result.snapshotLength) {
    for (i = _i = start, _ref = start + 400; start <= _ref ? _i <= _ref : _i >= _ref; i = start <= _ref ? ++_i : --_i) {
      setLink(result.snapshotItem(i));
    }
    setTimeout(function() {
      return linkPack(result, start + 400);
    }, 100);
  } else {
    for (i = _j = start, _ref1 = result.snapshotLength; start <= _ref1 ? _j <= _ref1 : _j >= _ref1; i = start <= _ref1 ? ++_j : --_j) {
      setLink(result.snapshotItem(i));
    }
  }
};

linkify = function(doc) {
  var result;
  result = document.evaluate(xpath, doc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  return linkPack(result, 0);
};

LinkPage = function(root) {
  var tW;
  tW = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, {
    acceptNode: function(a) {
      if (!filter.test(a.parentNode.localName)) {
        return NodeFilter.FILTER_ACCEPT;
      }
    }
  }, false);
  while (tW.nextNode()) {
    setLink(tW.currentNode);
  }
};

observer = new window.MutationObserver(function(mutations) {
  var Node, mutation, _i, _j, _len, _len1, _ref;
  for (_i = 0, _len = mutations.length; _i < _len; _i++) {
    mutation = mutations[_i];
    _ref = mutation.addedNodes;
    for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) {
      Node = _ref[_j];
      LinkPage(Node);
    }
  }
});

observer.observe(document.body, {
  childList: true,
  subtree: true
});

setTimeout((function() {
  return linkify(document.body);
}), 200);