forked from ethanchristensen01/Ethan-Snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbbPress-TopicHighlighter
53 lines (52 loc) · 2.25 KB
/
bbPress-TopicHighlighter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// ==UserScript==
// @require http://code.jquery.com/jquery-3.5.0.min.js
// @name Topic Highlighter for bbPress
// @namespace https://emc.code.blog
// @version 1.0
// @description Add a colored flag to each post which matches a list of keywords
// @author Ethan Christensen
// @match https://wordpress.com/forums/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// case insensitive contains selector by Highway of Life (https://stackoverflow.com/questions/8746882/jquery-contains-selector-uppercase-and-lower-case-issue)
jQuery.expr[':'].icontains = function(a, i, m) {
return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0;
};
//Topic class
class Topic {
constructor(color, keywords) {
this.color = color;
this.keywords = keywords;
}
query(classToSearch) {
let selectors = this.keywords.map(topic => `${classToSearch}:icontains('${topic}')`).join(', ');
return jQuery(`${selectors}`);
}
highlight(classToSearch, parentClass) {
let query = this.query(classToSearch);
let color = this.color;
console.log(query);
query.each(
function() {
//in case we decide to add class instead of changing css: jQuery(this).closest('.topic').classList.add(this.topicClass);
jQuery(this).closest(parentClass).css("border-left", `5px solid ${color}`);
}
);
}
}
//Change colors and keywords for each priority here
const Topics = [
new Topic('#000', ['theme', 'widget', 'color', 'menu', 'tab', 'css', 'header', 'template', 'editor']),
new Topic('blue', ['jet', 'woo', 'grav', 'vault', 'akismet', 'spam']),
new Topic('lime', ['restor', 'recover']),
new Topic('orange', ['account', 'login', 'log in', 'suspend', 'report', 'ads', 'application', 'confirmation', 'password']),
new Topic('red', ['dns', 'domain', 'cancel', 'refund', 'bill', 'charge', 'card', 'url'])
]
for(var topicIndex in Topics){
let topic = Topics[topicIndex];
console.log(topic[0]);
topic.highlight('.bbp-topic-title', '.topic');
}
})();