【问题标题】:How to replace text on a webpage with javascript and applescript如何用 javascript 和 applescript 替换网页上的文本
【发布时间】:2016-08-12 05:52:12
【问题描述】:

以下 javascript 代码来自对此question 的回答,非常适合替换网页上的文本。在 Safari 和 Chrome 中使用 javascript 控制台已经产生了成功的结果。

function replaceTextOnPage(from, to){
    getAllTextNodes().forEach(function(node){
        node.nodeValue = node.nodeValue.replace(new RegExp(quote(from), 'g'), to);
    });

    function getAllTextNodes(){
        var result = [];

        (function scanSubTree(node){
            if(node.childNodes.length) 
                for(var i = 0; i < node.childNodes.length; i++) 
                    scanSubTree(node.childNodes[i]);
            else if(node.nodeType == Node.TEXT_NODE) 
                result.push(node);
        })(document);

        return result;
    }

    function quote(str){
        return (str+'').replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
    }
}

replaceTextOnPage('oldtext', 'newtext');

但是,当我将其保存为 replace_text.js 并尝试使用 Applescript 运行它时,在 Safari 和 Chrome 中它都会返回 missing value

tell application "Safari"
    activate
    tell the current tab of window 1 to do JavaScript "/Users/Me/ScriptFolder/replace_text.js"
end tell

我还尝试在转义引号和转义后直接从脚本编辑器中的tell块运行javascript,而不是使用replace_text.js文件,但这也会导致缺少值 em>

当我将 javascript 代码直接复制并粘贴到脚本编辑器中并尝试使用 javascript 运行程序运行它时,我收到 Error -2700: Script error。

第 15 行错误:ReferenceError: 找不到变量:文档

如果我在脚本中将文档定义为document = "http://example.com",则会收到错误消息:

第 12 行出错:TypeError: undefined is not an object (evalating 'node.childNodes.length')

谁能告诉我我做错了什么?如何使用 Applescript 运行相同的 javascript 代码?

谢谢

【问题讨论】:

    标签: javascript macos applescript


    【解决方案1】:

    do JavaScript 命令需要一个包含 JavaScript 代码的字符串,而不是文件路径。

    所以,你可以使用:

    set myJS to read "/Users/Me/ScriptFolder/replace_text.js" as «class utf8» -- encoding of the file is "utf-8"
    tell application "Safari"
        tell the current tab of window 1 to do JavaScript myJS
    end tell
    

    或者这个:

    set myJS to "function replaceTextOnPage(from, to){
        getAllTextNodes().forEach(function(node){
            node.nodeValue = node.nodeValue.replace(new RegExp(quote(from), 'g'), to);
        });
        return 'replaceTextOnPage(), Done' // just for testing, to avoid the missing value from the do JavaScript command
        function getAllTextNodes(){
            var result = [];
            (function scanSubTree(node){
                if(node.childNodes.length) 
                    for(var i = 0; i < node.childNodes.length; i++) 
                        scanSubTree(node.childNodes[i]);
                else if(node.nodeType == Node.TEXT_NODE) 
                    result.push(node);
            })(document);
            return result;
        }
        function quote(str){
            return (str+'').replace(/([.?*+^$[\\]\\(){}|-])/g, \"\\$1\");
        }
    }
    replaceTextOnPage('oldtext', 'newtext');"
    
    tell application "Safari"
        activate
        tell the current tab of window 1 to do JavaScript myJS
    end tell
    

    do JavaScript 命令返回缺失值 当函数不返回任何内容(replaceTextOnPage 函数不返回任何内容),这是正常行为。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多