Anilist more links (MAL/AniDB)

adds links to anilist/mal site to anilist (uses just the duckduckgo I'm feeling ducky feature with the anime name)

目前為 2022-01-08 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Anilist more links (MAL/AniDB)
// @namespace    https://gf.qytechs.cn/en/users/12725-alistair1231
// @version      0.2
// @description  adds links to anilist/mal site to anilist (uses just the duckduckgo I'm feeling ducky feature with the anime name)
// @author       Alistair1231
// @match        https://anilist.co/anime/*
// @icon         https://icons.duckduckgo.com/ip2/anilist.co.ico
// @grant       GM_addStyle
// @grant       unsafeWindow
// @require https://code.jquery.com/jquery-3.6.0.min.js
// @license GPL-3.0
// ==/UserScript==

(function () {
  "use strict";
  waitForKeyElements(".content h1", run);
  // unsafeWindow.addEventListener("hashchange", console.log("new"), false);
})();

// thanks https://phpcoder.tech/detect-url-change-in-javascript-without-refresh/
function waitForURLChange() {
  let lastUrl = location.href;
  new MutationObserver(() => {
    const url = location.href;
    if (url !== lastUrl) {
      lastUrl = url;
      run();
    }
  }).observe(document, { subtree: true, childList: true });
}

function run() {
  // adds external links field to sidebar if missing
  addExternalLinkField();
  let linkBar = jQuery("div.external-links h2~div");
  let name = jQuery(".content h1")[0].innerText;
  let mal =
    "https://duckduckgo.com/?q=!ducky+" + name + "+site%3Amyanimelist.net";
  let anidb = "https://duckduckgo.com/?q=!ducky+" + name + "+site%3Aanidb.net";
  if (document.getElementById("MAL-link") == null) {
    linkBar.prepend(createButton(mal, "MAL"));
  }
  if (document.getElementById("AniDB-link") == null) {
    linkBar.prepend(createButton(anidb, "AniDB"));
  }

  //   let kitsu = "https://duckduckgo.com/?q=!ducky+" + name + "+site%3Akitsu.io";
  //   if (document.getElementById("anidb-link") == null) {
  //     linkBar.prepend(createButton(kitsu, "kitsu"));
  //   }

  waitForURLChange();
}
function addExternalLinkField() {
  if (document.querySelector("div.external-links h2~div") == null) {
    let div = document.createElement("div");
    jQuery(div).attr("data-v-7a1f9df8", "");
    jQuery(div).attr("data-v-1c97ba07", "");
    jQuery(div).addClass("external-links");
    div.innerHTML =
      '<h2 data-v-7a1f9df8="">External &amp; Streaming links</h2> <div data-v-7a1f9df8="" class="external-links-wrap"></div>';
    document.querySelector("div .sidebar").append(div);
  }
}

function createButton(link, name) {
  let a = document.createElement("a");
  jQuery(a).attr("data-v-7a1f9df8", "");
  a.id = name + "-link";
  a.href = link;
  a.target = "_blank";
  jQuery(a).addClass("external-link");
  a.innerHTML = name;
  // make purple
  jQuery(a).css("background", "rgb(150, 59, 241)");
  return a;
}

///////////////////////////////////////////////////////////////////////////////
// Wait For Key Elements
// https://gist.githubusercontent.com/raw/2625891/waitForKeyElements.js
///////////////////////////////////////////////////////////////////////////////


/*--- waitForKeyElements():  A utility function, for Greasemonkey scripts,
    that detects and handles AJAXed content.

    Usage example:

        waitForKeyElements (
            "div.comments"
            , commentCallbackFunction
        );

        //--- Page-specific function to do what we want when the node is found.
        function commentCallbackFunction (jNode) {
            jNode.text ("This comment changed by waitForKeyElements().");
        }

    IMPORTANT: This function requires your script to have loaded jQuery.
*/
function waitForKeyElements (
    selectorTxt,    /* Required: The jQuery selector string that
                        specifies the desired element(s).
                    */
    actionFunction, /* Required: The code to run when elements are
                        found. It is passed a jNode to the matched
                        element.
                    */
    bWaitOnce,      /* Optional: If false, will continue to scan for
                        new elements even after the first match is
                        found.
                    */
    iframeSelector  /* Optional: If set, identifies the iframe to
                        search.
                    */
) {
    var targetNodes, btargetsFound;

    if (typeof iframeSelector == "undefined")
        targetNodes     = $(selectorTxt);
    else
        targetNodes     = $(iframeSelector).contents ()
                                           .find (selectorTxt);

    if (targetNodes  &&  targetNodes.length > 0) {
        btargetsFound   = true;
        /*--- Found target node(s).  Go through each and act if they
            are new.
        */
        targetNodes.each ( function () {
            var jThis        = $(this);
            var alreadyFound = jThis.data ('alreadyFound')  ||  false;

            if (!alreadyFound) {
                //--- Call the payload function.
                var cancelFound     = actionFunction (jThis);
                if (cancelFound)
                    btargetsFound   = false;
                else
                    jThis.data ('alreadyFound', true);
            }
        } );
    }
    else {
        btargetsFound   = false;
    }

    //--- Get the timer-control variable for this selector.
    var controlObj      = waitForKeyElements.controlObj  ||  {};
    var controlKey      = selectorTxt.replace (/[^\w]/g, "_");
    var timeControl     = controlObj [controlKey];

    //--- Now set or clear the timer as appropriate.
    if (btargetsFound  &&  bWaitOnce  &&  timeControl) {
        //--- The only condition where we need to clear the timer.
        clearInterval (timeControl);
        delete controlObj [controlKey]
    }
    else {
        //--- Set a timer, if needed.
        if ( ! timeControl) {
            timeControl = setInterval ( function () {
                    waitForKeyElements (    selectorTxt,
                                            actionFunction,
                                            bWaitOnce,
                                            iframeSelector
                                        );
                },
                300
            );
            controlObj [controlKey] = timeControl;
        }
    }
    waitForKeyElements.controlObj   = controlObj;
}

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址