Fanfiction.net: Not-Crossover Link

Add link to not-Crossover page to Fanfiction.net.

目前為 2017-12-21 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Fanfiction.net: Not-Crossover Link
// @namespace    https://greasyfork.org/en/users/163551-vannius
// @version      1.0
// @description  Add link to not-Crossover page to Fanfiction.net.
// @author       Vannius
// @match        https://www.fanfiction.net/crossovers/*
// @include      https://www.fanfiction.net/*Crossovers/*
// @grant        none
// ==/UserScript==

(function () {
        // Functions
        // Replace and delete prohibited characters.
        function deleteProhibitedCharacter(text){
            return text.replace(/['|\.|\/| & | - | + ]+/g, ' ').replace(/[,\+!:;\|☆★!?]+/g, '');
        }

        // Return imgTag with click event
        function addClickToNotCrossover(imgTag, url){
            imgTag.addEventListener('click', function(e) {
                e.preventDefault();
                fetch(url, { method: 'GET',
                                 mode: 'same-origin',
                                 cache: 'default' })
                    .then(response => response.text())
                    .then(body => {
                        const doc = document.implementation.createHTMLDocument('myBody');
                        doc.documentElement.innerHTML = body;
                        const content = doc.getElementById('content_wrapper_inner');

                        const list = content.getElementsByClassName('z-list');
                        if (list.length) window.location.href = url; // Bingo

                        const candidates = content.querySelectorAll('.gui_normal a');
                        if (candidates.length == 1) window.location.href = candidates[0].href;
                        else if (candidates.length > 1) window.location.href = url; // There can be sevaral candidates for same fandom
                 }).catch(error=> console.log(error));
            });
            return imgTag;
        }

        // Main
        const splitPath = window.location.href.split('/');

        if (/^.+[-\_]and[-\_].+[-\_]Crossovers$/.test(splitPath[3])){
            // Scrape each fandom link
            const divTag = document.getElementById('content_wrapper_inner');
            const titleTags = [divTag.children[1], divTag.children[2]];

            for (let titleTag of titleTags){
                // Make joinTitle by replacing and deleting prohibited symbols
                const joinTitle = deleteProhibitedCharacter(titleTag.textContent).split(' ').join('-');
                const url = [window.location.origin, 'anime', joinTitle, ''].join('/');

                // Make link to Not-Crossover
                const imgTag = document.getElementById('content_wrapper_inner').children[0];
                const addImgTag = addClickToNotCrossover(imgTag.cloneNode(false), url);
                addImgTag.title = "Not-Crossover";
                addImgTag.style.transform = "scale(-1, 1)";

                // Add link and place adjustment
                imgTag.parentNode.insertBefore(addImgTag, titleTag);
                imgTag.parentNode.insertBefore(document.createTextNode(' '), titleTag);
            }
        }else if (splitPath[3] == 'crossovers' || /^.+[-\_]Crossovers$/.test(splitPath[3])){
            // Make title and url by splitPath[4] or splitPath[3]
            const title = (splitPath[3] == 'crossovers') ? splitPath[4] : splitPath[3].slice(0, - "-Crossovers".length);
            const url = [window.location.origin, 'anime', title, ''].join('/');

            // Make link to Not-Crossover
            const divTag = document.getElementById('content_wrapper_inner');
            const imgTag = (splitPath[3] == 'crossovers') ? divTag.children[2]: divTag.children[0];
            const addImgTag = addClickToNotCrossover(imgTag.cloneNode(false), url);
            addImgTag.title = "Not-Crossover";
            addImgTag.style.transform = "scale(-1, 1)";

            // Add link and place adjustment
            imgTag.parentNode.insertBefore(addImgTag, imgTag.nextSibling);
            imgTag.parentNode.insertBefore(document.createTextNode(' '), imgTag.nextSibling);
        }
})();