[英]highlight keywords in a chatbox, how to add different styles to several keywords?
I'm editing a userscript that has a highlight function inside a chat box on a gaming site. 我正在编辑一个在游戏网站的聊天框中具有突出显示功能的用户脚本。 I'm trying to make it so I can add keywords and different css styles to it.
我正在尝试使其能够为它添加关键字和不同的CSS样式。 I'm looking for something that adds keywords into groups, so that I can make one style to several keywords in that group.
我正在寻找将关键字添加到组中的内容,以便可以为该组中的多个关键字制作一种样式。 And a different style to another group.
和另一组的风格不同。
This is how it looks like 看起来像这样
http://i.imgur.com/7nbMuA4.png http://i.imgur.com/7nbMuA4.png
dctools.checkForChatKeyword = function(elements) {
if (GM_getValue("dct-options-hiflags") != null) {
$(elements).highlight('flag')
.highlight('keyword 1')
.highlight('keyword 2')
.highlight('keyword 3')
.highlight('keyword 4')
.highlight('keyword 5')
.highlight('keyword 6')
.highlight('keyword 7');
}
Is there a simple way to add css styles to these different keywords using arrays? 有没有一种简单的方法可以使用数组向这些不同的关键字添加CSS样式?
In general , (since you don't show the .highlight
function code) you would: 通常 ,(因为您没有显示
.highlight
函数代码)您将:
Create a series of jQuery style objects, like: 创建一系列jQuery样式对象,例如:
var styleRedOnYellow = {"color": "red", "background": "yellow"} , styleWhiteOnBlack = {"color": "white", "background": "black"} , styleLimeOnPink = {"color": "lime", "background": "pink"} , stylePuke_1 = {"color": "#EEF272", "background": "#97CC9A"} ;
Then you can link keywords with styles using an associative array : 然后,您可以使用关联数组将关键字与样式链接起来 :
var stylePerKeyword = []; stylePerKeyword["keyword 1"] = styleRedOnYellow; stylePerKeyword["apple"] = styleRedOnYellow; stylePerKeyword["keyword 3"] = styleLimeOnPink; stylePerKeyword["keyword 4"] = styleLimeOnPink; stylePerKeyword["shoes"] = stylePuke_1;
Then inside the .highlight
function, it can apply the styles to the wrapping nodes something like so: 然后在
.highlight
函数中,它可以将样式应用于包装节点,如下所示:
/*--- keywordParameter is the variable inside highlight() that contains the particular keyword for this function call. */ var styleToApply = stylePerKeyword[keywordParameter]; if (styleToApply) { /*-- wrappingNode is a jQuery around whatever highlight() wraps a word in to highlight it with. Probably a <span>. */ wrappingNode.css (styleToApply); } else { console.log ( '*** Unknown keyword, "' + keywordParameter + '", in .highlight()' ); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.