简体   繁体   English

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

[英]greasemonkey userscript injects variables within another variable

So currently I am trying to add a feature to a userscript I have made. 因此,当前我正在尝试向已创建的用户脚本中添加功能。

Currently the userscript will get some text into a variable. 当前,用户脚本会将一些文本放入变量中。 Now there are also have 2 vars, that currently are for adding text to the begenning and end of the text in the first var. 现在也有2个变种,当前用于将文本添加到第一个变种的文本的开头和结尾。 Here is a code sample to make you better understand what it is currently doing 这是一个代码示例,可让您更好地了解其当前正在执行的操作

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

Where elmTextarea is the string that the userscript got and opening and closing are the things I put in the beginning and end of it. 其中elmTextarea是用户脚本获得的字符串,打开和关闭是我在其开头和结尾处输入的内容。

Now, what I want to happen is that if the variable elmTextarea includes any [quote*]blahblahblah[/quote] it essentially will exclude those areas(not the star could be anything, it is in the format 现在,我想发生的是,如果变量elmTextarea包含任何[quote *] blahblahblah [/ quote],则它实际上将排除这些区域(不是星号可以是任何东西,它的格式是

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

but could also be just [quote] 但也可以只是[quote]

So here is a quick example so you can better understand 所以这是一个简单的例子,您可以更好地了解

if elmTextarea is 如果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

it would become 它会变成

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

So I have an idea as to how this would work, and here is my attempted implementation 所以我对这将如何工作有一个想法,这是我尝试的实现

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);

but adding these lines into my code makes my whole script not work. 但是将这些行添加到我的代码中会使整个脚本无法正常工作。 Any ideas as to why this does not work and how to fix it. 关于为什么此方法不起作用以及如何解决的任何想法。

Square brackets have a special meaning in a Regular expression. 方括号在正则表达式中具有特殊含义。 These should also be escaped: 这些也应该逃脱:

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