Greasy Fork is available in English.

JD Relay URL Modifier

Append __wsmode__=9 to relay*.jd.com/file/design?xxx URLs

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください。
// ==UserScript==
// @name         JD Relay URL Modifier
// @namespace    http://tampermonkey.net/
// @version      0.3.4
// @description  Append __wsmode__=9 to relay*.jd.com/file/design?xxx URLs
// @author       Your Name
// @match        *://relay.jd.com/file*
// @match        *://relay-test.jd.com/file*
// @match        *://relay0.jd.com/file*
// @match        *://ling.jd.com/file*
// @match        *://ling-design.jd.com/file*
// @match        *://ling-test.jd.com/file*
// @grant        none
// @license      MIT
// ==/UserScript==

;(function () {
  "use strict"
  const inFile = window.location.pathname.startsWith("/file/design")
  // 从 localStorage 获取状态
  let isEnabled = localStorage.getItem("wsmodeEnabled") === "true"

  // 创建按钮
  let button = document.createElement("button")
  button.innerText = isEnabled ? "单身模式: 开" : "单身模式: 关"
  button.style.position = "fixed"
  button.style.bottom = "60px"
  button.style.right = "10px"
  button.style.zIndex = "1000"
  document.body.appendChild(button)

  // 设置按钮初始状态
  button.style.backgroundColor = isEnabled ? "green" : "red"

  // 按钮点击事件
  button.addEventListener("click", () => {
    const previousState = isEnabled;
    isEnabled = !isEnabled;
    localStorage.setItem("wsmodeEnabled", isEnabled);
    button.style.backgroundColor = isEnabled ? "green" : "red";
    button.innerText = isEnabled ? "单身模式: 开" : "单身模式: 关";
    // 通知其他页面
    localStorage.setItem("wsmodeChanged", Date.now());

    // 如果在 inFile 页面并且 wsmode 状态有变化,刷新页面
    if (inFile && previousState !== isEnabled) {
        if (isEnabled && !url.searchParams.has("__wsmode__")) {
            url.searchParams.append("__wsmode__", "9");
        } else if (!isEnabled && url.searchParams.has("__wsmode__")) {
            url.searchParams.delete("__wsmode__");
        }
        window.location.replace(url.toString());
    }
});

  // 获取当前 URL
  let url = new URL(window.location.href)

  // 检查是否已经有 __wsmode__ 参数并且功能开启
  if (inFile) {
      if (isEnabled && !url.searchParams.has("__wsmode__")) {
          // 添加 __wsmode__ 参数
          url.searchParams.append("__wsmode__", "9")
          // 重定向到新的 URL
          window.location.replace(url.toString())
      }
  }

  // 监听新打开的 /file/design 页面
  window.addEventListener("message", (event) => {
    console.log("Received message from relay.jd.com:", event)
    if (
      event.origin === location.origin &&
      event.data === "checkWsmode"
    ) {
      event.source.postMessage({ wsmodeEnabled: isEnabled }, event.origin)
    }
  })

  // 在 /file/design 页面中检查 __wsmode__ 参数
  if (inFile) {
    window.addEventListener("message", (event) => {
      if (
        event.origin === location.origin &&
        typeof event.data.wsmodeEnabled !== "undefined"
      ) {
        if (event.data.wsmodeEnabled) {
          if (!url.searchParams.has("__wsmode__")) {
            url.searchParams.append("__wsmode__", "9")
            window.location.replace(url.toString())
          }
        } else {
          if (url.searchParams.has("__wsmode__")) {
            url.searchParams.delete("__wsmode__")
            window.location.replace(url.toString())
          }
        }
      }
    })
  }

  // 监听 localStorage 变化
  window.addEventListener("storage", (event) => {
    if (event.key === "wsmodeChanged") {
      isEnabled = localStorage.getItem("wsmodeEnabled") === "true"
      button.style.backgroundColor = isEnabled ? "green" : "red"
      button.innerText = isEnabled ? "单身模式: 开" : "单身模式: 关"
      if (inFile) {
        if (isEnabled && !url.searchParams.has("__wsmode__")) {
          url.searchParams.append("__wsmode__", "9")
          window.location.replace(url.toString())
        } else if (!isEnabled && url.searchParams.has("__wsmode__")) {
          url.searchParams.delete("__wsmode__")
          window.location.replace(url.toString())
        }
      }
    }
  })
})()