油腔滑调用户脚本在另一个变量中注入变量

greasemonkey userscript injects variables within another variable

本文关键字:变量 注入 另一个 用户 脚本 油腔滑调      更新时间:2023-09-26

所以目前我正在尝试将一个功能添加到我制作的用户脚本中。

目前,用户脚本将把一些文本放入变量中。现在还有两个变量,目前用于将文本添加到第一个变量中文本的开头和结尾。下面是一个代码示例,可以让您更好地了解它目前在做什么

elmTextarea.value   = opening + elmTextarea.value + closing;

elmTextarea是用户脚本得到的字符串,打开和关闭是我放在它的开头和结尾的东西

现在,我想做的是,如果变量elmTextarea包含任何[quote*]blashblash[/quote],它基本上会排除这些区域(而不是星形,它的格式是

[quote='name' pid='20784507' dateline='1331755619'])

但也可能只是

所以这里有一个快速的例子,这样你就可以更好地理解

如果elmTextarea是

blahbla
[quote='name' pid='20784507' dateline='1331755619']
some more text
[/quote]
here is some more
[quote='name' pid='20523454507' dateline='1335435619']
some more text in here
[/quote]
and finally this text

它将成为

opening + blahbla + closing
[quote='name' pid='20784507' dateline='1331755619']
some more text
[/quote]
opening + here is some more + closing
[quote='name' pid='20523454507' dateline='1335435619']
some more text in here
[/quote]
opening + and finally this text + closing

因此,我对这将如何工作有了一个想法,下面是我尝试实现的

var openingquote = closing + "[quote";
var closingquote = "[/quote]" + opening;
elmTextarea.value   = opening + elmTextarea.value + closing;
elmTextarea.value = elmTextarea.value.replace(/[quote/gim, openingquote);
elmTextarea.value = elmTextarea.value.replace(/['/quote]/gim, closingquote);

但是将这些行添加到我的代码中会使我的整个脚本无法工作。关于为什么这不起作用以及如何修复它的任何想法。

方括号在正则表达式中具有特殊含义。这些也应该逃脱:

elmTextarea.value = elmTextarea.value.replace(/'[quote/gim, openingquote);
elmTextarea.value = elmTextarea.value.replace(/'['/quote]/gim, closingquote);
//                                             ^ Escape [ using '[