繁体   English   中英

oilmonkey用户脚本将变量注入另一个变量中

[英]greasemonkey userscript injects variables within another variable

因此,当前我正在尝试向已创建的用户脚本中添加功能。

当前,用户脚本会将一些文本放入变量中。 现在也有2个变种,当前用于将文本添加到第一个变种的文本的开头和结尾。 这是一个代码示例,可让您更好地了解其当前正在执行的操作

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

其中elmTextarea是用户脚本获得的字符串,打开和关闭是我在其开头和结尾处输入的内容。

现在,我想发生的是,如果变量elmTextarea包含任何[quote *] blahblahblah [/ quote],则它实际上将排除这些区域(不是星号可以是任何东西,它的格式是

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

但也可以只是[quote]

所以这是一个简单的例子,您可以更好地了解

如果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 \[

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM