javascript 使用 File.write() 覆盖自身是否安全?
这是 Adobe 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不熟悉 Adobe 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.
几乎可以肯定,包含程序自我替换的能力将是一个糟糕的设计决策。首先,您会遇到困难,因为当前执行的线程与应该运行的文件不同,因此您肯定会遇到副作用。
基本上你是在寻求混乱,而混乱是不好的。您应该使用外部文件来存储数据,或者仅将信息保存在 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.