簡體   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