Zotero Plugins Install/Update

自动检查更新Zotero插件

当前为 2023-10-12 提交的版本,查看 最新版本

// ==UserScript==
// @name         Zotero Plugins Install/Update
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  自动检查更新Zotero插件
// @author       Polygon
// @match        https://plugins.zotero-chinese.com/
// @icon         https://www.google.com/s2/favicons?sz=64&domain=zotero-chinese.com
// @grant        GM_xmlhttpRequest
// @grant        unsafeWindow
// @run-at       document-end
// ==/UserScript==

(function () {
  'use strict';

  // zotero://plugin/?action=install&url=https%3A%2F%2Fgithub.com%2Fvolatile-static%2FChartero%2Freleases%2Fdownload%2F2.0.0%2Fchartero.xpi
  setTimeout(() => {
    GM_xmlhttpRequest({
      method: "POST",
      url: "http://127.0.0.1:23119/getAllPluginVersion",
      headers: {
        "Content-Type": "application/json",
      },
      responseType: "json",
      onload: function (res) {
        const addons = res.response;
        console.log(addons);
        [...document.querySelectorAll("tr td")]
          .filter(i => i.innerText == "7")
          .forEach(td => {
            const a = td.parentNode.children[0].querySelector("a") || td.parentNode.previousElementSibling.children[0].querySelector("a")
            const homepageURL = a.getAttribute("href")
            const addon = addons.find(addon => addon.homepageURL.toLowerCase() == homepageURL.toLowerCase())
            const aArr = [...td.parentNode.children].slice(-1)[0].querySelectorAll("a")
            if (addon) {
              const version = td.nextElementSibling.innerText
              console.log(addon.id, "最新版本", version, "安装版本", addon.version)
              if (version.replace(/^v/, "") > addon.version.replace(/^v/, "")) {
                aArr.forEach(a => {
                  const href = a.getAttribute("href")
                  a.innerText = `从${a.innerText}更新`;
                  a.style.color="deeppink"
                  a.setAttribute("href", `zotero://plugin/?action=install&url=${encodeURIComponent(href)}`)
                })
              } else {
                [...td.parentNode.children].slice(-1)[0].innerText = "已安装"
              }
            } else {
              aArr.forEach(a => {
                const href = a.getAttribute("href")
                a.innerText = `从${a.innerText}安装`;
                a.setAttribute("href", `zotero://plugin/?action=install&url=${encodeURIComponent(href)}`)
              })
            }
          })

      }
    })
  }, 1000)

})();