NJU-Auto-IDP

Auto pass NJU IDP check in some academic websites

目前为 2024-11-05 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name NJU-Auto-IDP
  3. // @namespace Flying-Tom/NJU-Auto-IDP
  4. // @version 0.0.1
  5. // @author Flying-Tom
  6. // @description Auto pass NJU IDP check in some academic websites
  7. // @license MIT
  8. // @icon https://z1.ax1x.com/2023/11/21/pia2Gtg.png
  9. // @match https://ieeexplore.ieee.org/*document/*
  10. // @match https://dl.acm.org/doi/*
  11. // @match https://www.sciencedirect.com/*
  12. // @grant GM_info
  13. // ==/UserScript==
  14.  
  15. (function () {
  16. 'use strict';
  17.  
  18. var _GM_info = /* @__PURE__ */ (() => typeof GM_info != "undefined" ? GM_info : void 0)();
  19. function promiseTimeout(promise, delay) {
  20. const timeout = new Promise(function(_reslove, reject) {
  21. setTimeout(() => {
  22. reject(new Error("Promise timeout"));
  23. }, delay);
  24. });
  25. return Promise.race([timeout, promise]);
  26. }
  27. function waitForElm(selector) {
  28. return new Promise((resolve) => {
  29. if (document.querySelector(selector)) {
  30. return resolve(document.querySelector(selector));
  31. }
  32. const observer = new MutationObserver(() => {
  33. if (document.querySelector(selector)) {
  34. resolve(document.querySelector(selector));
  35. observer.disconnect();
  36. }
  37. });
  38. observer.observe(document.body, {
  39. childList: true,
  40. subtree: true
  41. });
  42. });
  43. }
  44. function acm_handler() {
  45. promiseTimeout(waitForElm(".institution__name"), 200).then(function(data) {
  46. console.log(data);
  47. console.log("already login");
  48. }, function(err) {
  49. console.log("need to login");
  50. console.error(err);
  51. const match = window.location.href.match(/\/doi\/abs\/[a-zA-Z0-9./]+/);
  52. const redirectUri = match ? match[0] : "";
  53. const params = new URLSearchParams({
  54. idp: "https://idp.nju.edu.cn/idp/shibboleth",
  55. redirectUri,
  56. federationId: "urn:mace:shibboleth:carsifed"
  57. });
  58. window.location.href = "https://dl.acm.org/action/ssostart?" + params.toString();
  59. });
  60. }
  61. function ieee_handler() {
  62. promiseTimeout(waitForElm(".inst-name"), 200).then(function(data) {
  63. console.log(data);
  64. console.log("already login");
  65. }, function(err) {
  66. console.log("need to login");
  67. console.error(err);
  68. const params = new URLSearchParams({
  69. entityId: "https://idp.nju.edu.cn/idp/shibboleth",
  70. url: window.location.href
  71. });
  72. window.location.href = "https://ieeexplore.ieee.org/servlet/wayf.jsp?" + params.toString();
  73. });
  74. }
  75. function academic_handler() {
  76. if (window.location.href.includes("ieee.org/abstract/document")) {
  77. ieee_handler();
  78. } else if (window.location.href.includes("acm.org")) {
  79. acm_handler();
  80. } else if (window.location.href.includes("sciencedirect")) {
  81. return;
  82. }
  83. }
  84. (function() {
  85. (function() {
  86. 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();
  87. const strategy_load = {
  88. "0": academic_handler,
  89. // ieee 自动登录
  90. "1": academic_handler,
  91. // acm 自动登录
  92. "2": academic_handler
  93. // sciencedirect 自动登录
  94. };
  95. const strategy_instant = {};
  96. if (match_idx in strategy_instant) {
  97. const strategy_instant_func = strategy_instant[match_idx];
  98. strategy_instant_func();
  99. } else if (match_idx in strategy_load) {
  100. const strategy_load_func = strategy_load[match_idx];
  101. if (document.readyState == "complete") {
  102. strategy_load_func();
  103. } else {
  104. window.addEventListener("load", strategy_load_func);
  105. }
  106. }
  107. })();
  108. })();
  109.  
  110. })();