iwara 一键搜索

一键在mmdfans.net中搜索MMD

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         iwara 一键搜索
// @name:en      iwara one-click search
// @namespace    http://tampermonkey.net/
// @version      0.6
// @description  一键在mmdfans.net中搜索MMD
// @description:en  one-click search MMD in mmdfans.net
// @author       XiChan
// @match        *://www.iwara.tv/*
// @match        *://www.iwara.tv
// @icon         https://www.google.com/s2/favicons?sz=64&domain=iwara.tv
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    var addSearchButtonTask = null;

    function tryAddSearchButton() {
        console.log("iwara 一键搜索 启动");
        // Get element by class name is page-video__details
        // var videoDetails = document.getElementsByClassName("page-video__details")[0];
        // need wait for the element to be loaded
        if (addSearchButtonTask) {
            addSearchButtonTask.cancel();
        }

        addSearchButtonTask = new Promise((resolve, reject) => {
            (function wait() {
                var videoDetails = document.getElementsByClassName("page-video__details")[0];
                if (videoDetails) {
                    resolve(videoDetails);
                } else {
                    setTimeout(wait, 100);
                }
            })();
        }).then((videoDetails) => {
            // The first div in page-video__details is the title
            var videoTitle = videoDetails.getElementsByTagName("div")[0].innerText;

            // Create a new button, append it after the title
            var searchButton = document.createElement("button");
            searchButton.innerText = "在MMDFans搜索";
            searchButton.style = "margin-left: 10px; background-color: #4CAF50; border: none; color: white; padding: 5px 10px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer;";
            videoDetails.appendChild(searchButton);

            // Click event, open link https://mmdfans.net/?query=title:{url_encoded_title}&order_by=time
            searchButton.onclick = function () {
                var url = "https://mmdfans.net/?query=title:" + encodeURIComponent(videoTitle) + "&order_by=time";
                window.open(url);
            }
        });
    }

    function maybeLoadSearchButton() {
        // check url is start with www.iwara.tv/video/
        if (window.location.href.startsWith("https://www.iwara.tv/video/") || window.location.href.startsWith("http://www.iwara.tv/video/")) {
            tryAddSearchButton();
        }
    }

    // Listening url change
    var oldUrl = "";
    setInterval(function () {
        if (oldUrl != window.location.href) {
            oldUrl = window.location.href;
            maybeLoadSearchButton();
        }
    }, 1000);

})();