javascript 使用 File.write() 覆盖自身是否安全?

发布于 2024-10-29 08:23:30 字数 827 浏览 5 评论 0原文

这是 Adob​​e ExtendScript 的 javascript。基本上,我希望在脚本中有一个持久变量来存储用户的首选项,就像使用 AppleScript property 一样。我能想到的唯一方法是让脚本用 File.write() 覆盖自身。

var MY_PROPERTY = true;

function reassignProperty(propName, propValue) {
    var thisFile = new File($.fileName);

    if (thisFile.open("r")) {
        var myScript = thisFile.read();
        thisFile.close();

        // Look for the property declaration and overwrite with new value
        var searchStr = new RegExp("(var " + propName + " = )" + ".+");
        var newScript = myScript.replace(searchStr, "$1" + propValue + ";");

        thisFile.open("w");
        thisFile.write(newScript);
        thisFile.close();
    }
}

reassignProperty("MY_PROPERTY", "false");

据我所知,这有效。但这安全吗?我的直觉告诉我应该有一种更干净的方法来保存持久变量,但如果没有,我只想知道自覆盖脚本是否存在任何潜在问题。

This is a javascript for Adobe ExtendScript. Basically I want to have a persistent variable in the script to store a user's preferences, like you can with an AppleScript property. The only way I could think of was for the script to overwrite itself with File.write().

var MY_PROPERTY = true;

function reassignProperty(propName, propValue) {
    var thisFile = new File($.fileName);

    if (thisFile.open("r")) {
        var myScript = thisFile.read();
        thisFile.close();

        // Look for the property declaration and overwrite with new value
        var searchStr = new RegExp("(var " + propName + " = )" + ".+");
        var newScript = myScript.replace(searchStr, "$1" + propValue + ";");

        thisFile.open("w");
        thisFile.write(newScript);
        thisFile.close();
    }
}

reassignProperty("MY_PROPERTY", "false");

As far as I could tell, this worked. But is it safe? My gut instinct tells me there should be a cleaner way to have persistent variables, but if there isn't, I just want to know if there are any potential problems with a self-overwriting script.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一抹微笑 2024-11-05 08:23:30

我不熟悉 Adob​​e ExtendScript,但通常您会将用户的首选项存储在单独的文件中,然后在程序启动时读取该文件。

I am not familiar with Adobe ExtendScript, but usually you would store the user's preferences in a separate file and then read that file when the program starts.

一场春暖 2024-11-05 08:23:30

几乎可以肯定,包含程序自我替换的能力将是一个糟糕的设计决策。首先,您会遇到困难,因为当前执行的线程与应该运行的文件不同,因此您肯定会遇到副作用。

基本上你是在寻求混乱,而混乱是不好的。您应该使用外部文件来存储数据,或者仅将信息保存在 HTML 的变量或字段中。

It's almost certainly going to be a bad design decision to include the ability for a program to replace itself. First off you'd have a difficulties because the current thread of execution differs from the file that's supposed to be running, so you'd definitely get side-effects.

Basically your asking for confusion, and confusion is bad. You should use an external file to store the data, or just hold the info in a variable or field of HTML.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文