// ==UserScript==
// @name COLG功能增强
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author Aur3l14no
// @match https://bbs.colg.cn/*
// @require https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js
// @grant none
// @downloadURL none
// ==/UserScript==
(function ($, undefined) {
$(function () {
//Your code here;
const utils = {
// save user config
saveConfig: config => {
const configString = JSON.stringify(config);
localStorage.setItem('colg_enhance_config', configString);
},
// load user config
getConfig: () => {
const configString = localStorage.getItem('colg_enhance_config');
const oldConfigString = localStorage.getItem('colg_filter_config');
try {
const config = JSON.parse(configString || oldConfigString);
return config;
} catch (e) {
return {};
}
},
bindedEles: [],
bindClick: function (selector, callback) {
this.bindedEles.push(selector);
$(selector).click(callback);
},
unbindClick: (selector) => {
$(selector).unbind();
},
unbindAllClick: function () {
this.bindedEles.forEach(selector => {
$(selector).click(callback);
})
}
}
const createEnhancer = () => {
// run user filters
const runFilter = (config, self) => {
const title = self.find('a.s.xst').text();
const user = self.find('td.by > cite > a').text();
const isNewUserThread = self.find('img[alt="新人帖"]').length > 0;
const containsAnyKeyword = (text, keywords) => (keywords || []).find(keyword => text.indexOf(keyword) >= 0);
const matchesAnyKeyword = (text, keywords) => (keywords || []).find(keyword => text === keyword);
const isTitleIncluded = containsAnyKeyword(title, config.includedKeywords);
const isTitleExcluded = containsAnyKeyword(title, config.excludedKeywords);
const isUserExcluded = matchesAnyKeyword(user, config.excludedUsers);
if (isTitleExcluded || isUserExcluded || (true && isNewUserThread)) {
self.hide();
}
else if (isTitleIncluded) {
self.find('a.s.xst').addClass('colg_enhance_highlight');
}
}
const runEnhancer = () => {
const config = utils.getConfig();
const isThreadDetailPage = location.pathname.indexOf('/thread-') >= 0;
const search = location.search ? location.search.substr(1) : '';
const params = {};
search.split('&').filter(v => !!v).map(item => {
const items = item.split('=');
if (items.length >= 1) {
params[items[0]] = items[1];
}
});
const global = {
config: config,
params: params,
};
if (isThreadDetailPage) {
// 先什么都不做
} else {
// 帖子列表
$('tbody[id^="normalthread_"]').each(function () {
const $this = $(this);
runFilter(config, $this);
});
}
}
// init form elements
const initDom = () => {
// init config dom
let configDivHtml = `
COLG优化设置
感兴趣的关键词(空格隔开)
屏蔽的关键词(空格隔开)
屏蔽的用户(空格隔开)
`;
let styleHtml = `
`;
$(document.body).append(configDivHtml);
$(document.body).append(styleHtml);
// init config btn
const insertPos = $('.dfsj_nv_z > #um > p');
if (insertPos) {
insertPos.append('
|COLG增强插件设置');
}
// init load more btn
const scrollTopPos = $('#scrolltop');
if (scrollTopPos) {
scrollTopPos.append('
加载更多')
}
}
// init dom events
const initDomEvents = config => {
const $container = $('#colg_enhance_container');
const $body = $(document.body);
// bind events
utils.bindClick('#colg_enhance_show_config', e => {
$container.show();
$body.css('overflow', 'hidden');
});
utils.bindClick('#colg_enhance_cancel', e => {
$container.hide();
$body.css('overflow', 'initial');
});
utils.bindClick('.colg_enhance_mask', e => {
$container.hide();
$body.css('overflow', 'initial');
});
utils.bindClick('#colg_enhance_confirm', e => {
const config = {
includedKeywords: $('#colg_enhance_container textarea')[0].value.split(' ').filter(v => !!v),
excludedKeywords: $('#colg_enhance_container textarea')[1].value.split(' ').filter(v => !!v),
excludedUsers: $('#colg_enhance_container textarea')[2].value.split(' ').filter(v => !!v),
}
utils.saveConfig(config);
runEnhancer();
$container.hide();
$body.css('overflow', 'initial');
});
utils.bindClick('a.bm_h', e => {
const doRunEnhancer = setInterval(function () {
if ($('a.bm_h').text() != '正在加载, 请稍后...') {
runEnhancer();
clearInterval(doRunEnhancer);
}
}, 200);
});
utils.bindClick('#loadmore', e => {
loadPages(5);
});
}
const loadPages = pageNum => {
for (var i = 0; i < pageNum; i++) {
setTimeout(function () {
$('a.bm_h').click();
}, i * 1000);
}
}
// init form values
const initDomValue = config => {
$('#colg_enhance_container textarea')[0].value = (config.includedKeywords || []).join(' ');
$('#colg_enhance_container textarea')[1].value = (config.excludedKeywords || []).join(' ');
$('#colg_enhance_container textarea')[2].value = (config.excludedUsers || []).join(' ');
}
const init = () => {
const config = utils.getConfig() || {};
initDom();
initDomValue(config);
initDomEvents();
runEnhancer();
}
const destory = () => {
// remove dom events
utils.unbindAllClick();
// remove all added elements
$('#.colg_added').remove();
}
return {
init,
destory,
_version: '0.1'
}
}
// init
if (window.colgEnchancer) {
const enhancer = createEnhancer();
if (!colgEnchancer._version) {
colgEnchancer._version = '0'
}
if (window.colgEnchancer._version < enhancer._version) {
if (colgEnchancer.destory) {
colgEnchancer.destory();
}
window.colgEnchancer = enhancer;
colgEnchancer.init();
}
} else {
window.colgEnchancer = createEnhancer();
colgEnchancer.init();
}
})();
})(window.jQuery.noConflict(true));