NJU-Auto-IDP

Auto pass NJU IDP check in some academic websites

// ==UserScript==
// @name         NJU-Auto-IDP
// @namespace    Flying-Tom/NJU-Auto-IDP
// @version      0.0.1
// @author       Flying-Tom
// @description  Auto pass NJU IDP check in some academic websites
// @license      MIT
// @icon         https://z1.ax1x.com/2023/11/21/pia2Gtg.png
// @match        https://ieeexplore.ieee.org/*document/*
// @match        https://dl.acm.org/doi/*
// @match        https://www.sciencedirect.com/*
// @grant        GM_info
// ==/UserScript==

(function () {
  'use strict';

  var _GM_info = /* @__PURE__ */ (() => typeof GM_info != "undefined" ? GM_info : void 0)();
  function promiseTimeout(promise, delay) {
    const timeout = new Promise(function(_reslove, reject) {
      setTimeout(() => {
        reject(new Error("Promise timeout"));
      }, delay);
    });
    return Promise.race([timeout, promise]);
  }
  function waitForElm(selector) {
    return new Promise((resolve) => {
      if (document.querySelector(selector)) {
        return resolve(document.querySelector(selector));
      }
      const observer = new MutationObserver(() => {
        if (document.querySelector(selector)) {
          resolve(document.querySelector(selector));
          observer.disconnect();
        }
      });
      observer.observe(document.body, {
        childList: true,
        subtree: true
      });
    });
  }
  function acm_handler() {
    promiseTimeout(waitForElm(".institution__name"), 200).then(function(data) {
      console.log(data);
      console.log("already login");
    }, function(err) {
      console.log("need to login");
      console.error(err);
      const match = window.location.href.match(/\/doi\/abs\/[a-zA-Z0-9./]+/);
      const redirectUri = match ? match[0] : "";
      const params = new URLSearchParams({
        idp: "https://idp.nju.edu.cn/idp/shibboleth",
        redirectUri,
        federationId: "urn:mace:shibboleth:carsifed"
      });
      window.location.href = "https://dl.acm.org/action/ssostart?" + params.toString();
    });
  }
  function ieee_handler() {
    promiseTimeout(waitForElm(".inst-name"), 200).then(function(data) {
      console.log(data);
      console.log("already login");
    }, function(err) {
      console.log("need to login");
      console.error(err);
      const params = new URLSearchParams({
        entityId: "https://idp.nju.edu.cn/idp/shibboleth",
        url: window.location.href
      });
      window.location.href = "https://ieeexplore.ieee.org/servlet/wayf.jsp?" + params.toString();
    });
  }
  function academic_handler() {
    if (window.location.href.includes("ieee.org/abstract/document")) {
      ieee_handler();
    } else if (window.location.href.includes("acm.org")) {
      acm_handler();
    } else if (window.location.href.includes("sciencedirect")) {
      return;
    }
  }
  (function() {
    (function() {
      const match_idx = _GM_info.script.matches.map((rule) => rule.replace(/\.|\*|\/|\?/g, (match) => ({ ".": "\\.", "*": ".*", "/": "\\/", "?": "\\?" })[match] || "")).map((rule) => new RegExp(rule)).map((regExp, index) => regExp.test(window.location.href) ? index : null).filter((index) => index != null).join().toString();
      const strategy_load = {
        "0": academic_handler,
        // ieee 自动登录
        "1": academic_handler,
        // acm 自动登录
        "2": academic_handler
        // sciencedirect 自动登录
      };
      const strategy_instant = {};
      if (match_idx in strategy_instant) {
        const strategy_instant_func = strategy_instant[match_idx];
        strategy_instant_func();
      } else if (match_idx in strategy_load) {
        const strategy_load_func = strategy_load[match_idx];
        if (document.readyState == "complete") {
          strategy_load_func();
        } else {
          window.addEventListener("load", strategy_load_func);
        }
      }
    })();
  })();

})();