Enhancement Userscript for LIHKG

An Enhancement Userscript for LIHKG

Versione datata 10/06/2021. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Enhancement Userscript for LIHKG
// @version      0.1
// @description  An Enhancement Userscript for LIHKG
// @include        /https?\:\/\/lihkg\.com/
// @icon         https://www.google.com/s2/favicons?domain=lihkg.com
// @grant        none
// @namespace https://greatest.deepsurf.us/users/371179
// ==/UserScript==

(function() {
    'use strict';
document.cssAll = function() {

    var s = document.querySelectorAll.apply(this, arguments)

    s = Array.prototype.slice.call(s, 0)
    return s
}

function urlConvert(url) {
    var src = url.replace(/\w+\:\/\//, '')
    var replacements = [...src.matchAll(/[\w\.]+/g)].filter((t) => /\./.test(t))
    if (replacements.length > 1) {
        replacements.length--;

    }
    replacements.forEach((s) => {
        src = src.replace(s, '')
    })

    src = src.replace(/\/+/g, '/')

    return src;

}

var emoji = {};

setInterval(() => {

    document.cssAll('img[src*="lihkg.com"][alt]:not([title])').forEach(function(imgElm) {
        var src = imgElm.getAttribute('src');
        var erc = urlConvert(src)
        var imgAlt = imgElm.getAttribute('alt') || "";
        if (/^[\x20-\x7E]+$/.test(imgAlt) && /\#/.test(imgAlt)) {
            emoji[erc] = imgAlt.trim()
        }

        imgElm.setAttribute('title', imgAlt)

    })


    document.cssAll('a[href*="profile/"]:not([href*="//"]):not([title])').forEach(function(aElm) {
        aElm.setAttribute('title', aElm.getAttribute('href'))
    })

    document.cssAll('[data-ic~="hkgmoji"]:not([title])>img[src*="lihkg.com"]:not([alt])').forEach(function(imgElm) {
        var src = imgElm.getAttribute('src');
        var erc = urlConvert(src)
        var text = emoji[erc] ? emoji[erc] : "[img]" + erc + "[/img]"
        imgElm.parentNode.setAttribute('title', text)
        imgElm.setAttribute('alt', text)


    })

    document.cssAll('a[href*="local.lihkg.com"]>img:not([anchored])').forEach(function(img) {
        img.setAttribute('anchored', 'true')


        var originalSrc = img.getAttribute('src') || img.getAttribute('data-original') || ""
        var newSrc = originalSrc.replace('local.lihkg.com', 'cdn.lihkg.com');

        if (newSrc && originalSrc != newSrc) {

            console.log(originalSrc, newSrc)

            var fx = function() {
                if (img.complete == false) return setTimeout(fx, 33);

                if (img.currentSrc == "") {

                    var b = img.cloneNode(false);
                    b.removeAttribute('data-original');
                    b.removeAttribute('data-src');

                    if (b.getAttribute('alt') == "") b.removeAttribute('alt')
                    if (b.getAttribute('title') == "") b.removeAttribute('title')

                    b.setAttribute('src', newSrc);
                    img.parentNode.replaceChild(b, img)

                    if (b.parentNode.getAttribute('href')) {
                        b.parentNode.setAttribute('href', b.parentNode.getAttribute('href').replace(originalSrc, newSrc));
                        if (b.nextElementSibling && b.nextElementSibling.hasAttribute('data-error')) b.nextElementSibling.parentNode.removeChild(b.nextElementSibling);
                        if (b.nextElementSibling && b.nextElementSibling.outerHTML.toLocaleLowerCase() == '<ins></ins>') b.nextElementSibling.parentNode.removeChild(b.nextElementSibling);



                    }

                    b.removeAttribute('anchored')


                }

            }
            fx();


        }

    })


    document.cssAll('div[contenteditable] p>br:not([hacked])').forEach((brElm) => {
        brElm.setAttribute('hacked', 'true')
        if (brElm.nextElementSibling === null && brElm.parentNode.nextElementSibling && brElm.parentNode.nextElementSibling.nodeType == 1 && brElm.parentNode.nextElementSibling.tagName == 'P') {
            brElm.style.display = 'none'
            // brElm.parentNode.removeChild(brElm)
        }

    })


}, 33)

    // Your code here...
})();