douban auto-link

auto link for douban

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         douban auto-link
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  auto link for douban
// @author       yetone
// @match        https://*.douban.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    replace(document);

    document.querySelectorAll('.full-content').forEach(x => {
        x.addEventListener('DOMSubtreeModified', e => replace(e.target));
    });

    function replace(ele) {
        ele.querySelectorAll('p, blockquote, span, .short-content, .review-content').forEach(p => {
            let nodes = [];
            let need = false;
            p.childNodes.forEach(x => {
                if (x.nodeType !== 3) {
                    nodes.push(x);
                    return;
                }
                let text = x.textContent;
                let lastIdx = 0;
                text.replace(/《.+?》|「.+?」|『.+?』/g, (c, idx, t) => {
                    need = true;
                    nodes.push(new Text(t.substring(lastIdx, idx + 1)));
                    lastIdx = idx + c.length - 1;
                    let a = document.createElement('a');
                    let cc = t.substring(idx + 1, idx + c.length - 1);
                    a.href = `https://www.douban.com/search?q=${encodeURIComponent(cc)}`;
                    a.target = '_blank';
                    a.innerText = cc;
                    nodes.push(a);
                });
                nodes.push(new Text(text.substring(lastIdx)));
            });
            if (need) {
                while (p.childNodes.length > 0) {
                    p.childNodes.forEach(x => {
                        p.removeChild(x);
                    });
                }
                nodes.forEach(x => {
                    p.appendChild(x);
                });
            }
        });
    }
})();