V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
拼车信息请发到 /go/cosub 节点。 如果没有发送到 /go/cosub,那么会被移动到 /go/pointless。如果持续触发这样的移动,会导致账号被禁用。
wclebb
V2EX  ›  Surge

我想在 Surge iOS 执行脚本,然后替换某网页的关键字。

  •  
  •   wclebb · 2023-02-25 13:38:43 +08:00 · 604 次点击
    这是一个创建于 439 天前的主题,其中的信息可能已经有所发展或是发生改变。
    // 需要替换的关键字
    const targetText = "关键字";
    // 替换成的文本
    const replacementText = "被关键字替换";

    // 遍历所有文本节点,替换关键字
    function replaceText(node) {
    if (node.nodeType === Node.TEXT_NODE) {
    const text = node.textContent;
    const replacedText = text.replaceAll(targetText, replacementText);
    if (replacedText !== text) {
    node.textContent = replacedText;
    }
    } else if (node.nodeType === Node.ELEMENT_NODE) {
    const tagName = node.tagName.toLowerCase();
    if (tagName !== "script" && tagName !== "style") {
    const childNodes = node.childNodes;
    for (let i = 0; i < childNodes.length; i++) {
    replaceText(childNodes[i]);
    }
    }
    }
    }

    // 替换文本,这个貌似不执行也不影响结果。
    // replaceText(document.body);
    // Surge 必须要有 done 最后。
    $done()

    ——————

    我在 Chrome 执行后确实能替换。
    但在 Surge 报错,我不知道问题在哪。
    iX8NEGGn
        1
    iX8NEGGn  
       2023-02-25 14:58:39 +08:00 via iPhone
    你需要这样才行:

    let body = $response.body;

    //……对 body 进行替换的代码

    $done({ body });
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3250 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 12:40 · PVG 20:40 · LAX 05:40 · JFK 08:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.