[HFR] Links preview

Previsualise le contenu des liens dans les posts

当前为 2018-11-03 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         [HFR] Links preview
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Previsualise le contenu des liens dans les posts
// @icon         http://reho.st/self/40f387c9f48884a57e8bbe05e108ed4bd59b72ce.png
// @author       Garath_
// @include      https://forum.hardware.fr/forum2.php*
// @include      https://forum.hardware.fr/hfr/*
// @grant        GM_xmlhttpRequest
// ==/UserScript==


var auSurvol = true;

var links = document.querySelectorAll("td.messCase2 > div[id^='para'] > p > a.cLink, table.spoiler a.cLink");

function truncate(str, n)
{
    var isTooLong = str.length > n,
    s_ = isTooLong ? str.substr(0,n-1) : str;
    //s_ = isTooLong ? s_.substr(0,s_.lastIndexOf(' ')) : s_;
    return isTooLong ? s_ + '...' : s_;
}


function format(txt, href) {
    var parser = new DOMParser();
    var xmlDoc = parser.parseFromString(txt, 'text/html');

    const metas = xmlDoc.getElementsByTagName('meta');
    var des = "";
    var title = "";
    var image = "";
    var link = "";
    var url = "";
    var resolve = false;


    for (let i = 0; i < metas.length; i++) {
        if (metas[i].getAttribute('name') === "description") {
            des = metas[i].getAttribute('content');
        }
        if (metas[i].getAttribute('property') === "og:title") {
            title = metas[i].getAttribute('content');
        }
        if (metas[i].getAttribute('property') === "og:url") {
            link = metas[i].getAttribute('content');
        }
        if (metas[i].getAttribute('property') === "og:image") {
            image = metas[i].getAttribute('content');
        }
        if (metas[i].getAttribute('property') === "og:url") {
            url = metas[i].getAttribute('content');
        }
    }

    if (title === "") {
        var t = xmlDoc.getElementsByTagName('title')[0];
        if (t) {
            title = t.innerText;
        }
    }

    var newLink = document.createElement("div");
    newLink.style.cssText = "background-color: white; border:1px; border-style:solid; border-color:#C0C0C0; padding: 5px; overflow:hidden; max-width:500px;"

    if (image) {
        var img = document.createElement("img");
        img.setAttribute("src", image);
        img.style.cssText = "float:left; margin-right:3px; max-height:100px; max-width:100px";
        newLink.appendChild(img);

        resolve = true;
    }

    if (title) {
        var a = document.createElement("a");
        a.setAttribute("href", href);
        a.style.cssText = "color: black; font-weight: 550; font-size: 10px;";
        a.appendChild(document.createTextNode(truncate(title, 100)));
        newLink.appendChild(a);
    }

    if (url) {
        newLink.appendChild(document.createElement("br"));
        var s = document.createElement("a");
        s.setAttribute("href", url);
        s.style.cssText = "color: #808080; font-size: 0.8em;"
        s.appendChild(document.createTextNode(truncate(url, 50)));
        newLink.appendChild(s);

        resolve = true;
    }

    if (des) {
        if (des.localeCompare(title) != 0) {
            newLink.appendChild(document.createElement("br"));
            newLink.appendChild(document.createElement("br"));
            var desc = document.createElement("div");
            if (image) {
                desc.style.cssText = "margin-left: 103px;";
            }
            desc.appendChild(document.createTextNode(truncate(des, 150)));
            newLink.appendChild(desc);

            resolve = true;
        }
    }

    if (resolve) {
        return newLink;
    }
    else {
        return document.createElement("div");
    }
}

function requestContentOnMouse() {
    var target = this;
    GM_xmlhttpRequest({
            method: "GET",
            url: target.href,
            mozAnon: true,
            anonymous: true,
            onload: function(response) {
                var newLink = format(response.responseText, target.href);
                if (newLink.innerHTML != "") {
                    target.parentNode.replaceChild(newLink, target);
                }

            }
     });
}

function requestContent(link) {
    GM_xmlhttpRequest({
            method: "GET",
            url: link.href,
            mozAnon: true,
            anonymous: true,
            onload: function(response) {
                var newLink = format(response.responseText, link.href);
                if (newLink.innerHTML != "") {
                    link.parentNode.replaceChild(newLink, link);
                }
            }
     });
}

function replace(links) {
  for(let link of links) {
      if (auSurvol) {
          link.addEventListener("mouseover", requestContentOnMouse, false);
      }
      else {
          requestContent(link);
      }
  }
}

replace(links);