Skip to content

Instantly share code, notes, and snippets.

@OpenGG
Created October 19, 2017 06:16
Show Gist options
  • Save OpenGG/1a651b07033813d440a9531b847a9e06 to your computer and use it in GitHub Desktop.
Save OpenGG/1a651b07033813d440a9531b847a9e06 to your computer and use it in GitHub Desktop.
nga.blocker
// ==UserScript==
// @name nga.blocker
// @namespace http://ngacn.cc/
// @version 0.1
// @description Block threads by titles and authors
// @author OpenGG
// @match https://bbs.ngacn.cc/thread.php?fid=*
// @grant none
// ==/UserScript==
(function () {
'use strict';
var MatchWithArr = function MatchWithArr(fn) {
return function (compare, arr) {
for (var i = arr.length - 1; i > -1; --i) {
if (fn(compare, arr[i])) {
return true;
}
}
return false;
};
};
var random = Math.floor(Math.random() * Math.pow(2, 53));
var clsHide = '_hydra_hidden_nga_' + random;
var clsOptions = '_hydra_options_nga_' + random;
var clsBtn = '_hydra_btn_nga_' + random;
var css = '.' + clsHide + '{display:none}.' + clsBtn + '{position:absolute;top:100px;right:30px;opacity:0.6}.' + clsBtn + ':hover{opacity:1}.' + clsOptions + '{position:absolute;top:130px;right:30px;width:600px;padding:10px;background:#abc}.' + clsOptions + ' label{display:block}';
var addStyle = function addStyle() {
var style = document.createElement('style');
style.textContent = css;
(document.head || document.documentElement).appendChild(style);
};
var hideEl = function hideEl(el) {
el.classList.add(clsHide);
};
var blockedTopics = (localStorage.blockedTopics || '').split(/\r?\n/);
var blockedAuthors = (localStorage.blockedAuthors || '').split(/\r?\n/);
var block = function block() {
var rows = document.querySelectorAll('.topicrow');
var matchAuthor = MatchWithArr(function (compare, item) {
var matches = compare.match(/uid=(\d+)$/);
var uid = matches && matches[1];
return item === uid;
});
var matchTopic = MatchWithArr(function (compare, item) {
return compare.indexOf(item) !== -1;
});
for (var i = rows.length - 1; i > -1; --i) {
var row = rows[i];
if (blockedAuthors.length > 0) {
var author = row.querySelector('a.author');
if (matchAuthor(author.href, blockedAuthors)) {
hideEl(row);
continue;
}
}
if (blockedAuthors.length > 0) {
var topic = row.querySelector('.topic');
if (matchTopic(topic.textContent, blockedTopics)) {
hideEl(row);
continue;
}
}
}
};
var optionsShow = false;
var hideOptions = function hideOptions() {
optionsShow = false;
var el = document.querySelector('.' + clsOptions);
if (el) {
el.parentNode.removeChild(el);
}
};
var showOptions = function showOptions() {
optionsShow = true;
var container = document.createElement('div');
container.className = clsOptions;
container.innerHTML = '<label>屏蔽标题关键字(每行一个):<textarea placeholder="一行一个关键字"></textarea></label><label>屏蔽楼主UID(每行一个):<textarea placeholder="一行一个UID"></textarea></label><button>保存</button>';
var _container$querySelec = container.querySelectorAll('textarea'),
blockTopicEl = _container$querySelec[0],
blockAuthorEl = _container$querySelec[1];
var save = container.querySelector('button');
blockTopicEl.value = blockedTopics.join('\n');
blockAuthorEl.value = blockedAuthors.join('\n');
save.addEventListener('click', function () {
localStorage.blockedTopics = blockTopicEl.value;
localStorage.blockedAuthors = blockAuthorEl.value;
hideOptions();
location.reload();
}, false);
document.body.appendChild(container);
};
var addControls = function addControls() {
var el = document.createElement('button');
el.className = clsBtn;
el.textContent = '屏蔽设置';
el.onclick = function () {
if (optionsShow) {
hideOptions();
} else {
showOptions();
}
};
document.body.appendChild(el);
};
var init = function init() {
document.removeEventListener('DOMContentLoaded', init, false);
addStyle();
addControls();
block();
};
if (document.readyState === 'interactive' || document.readyState === 'complete') {
init();
} else {
document.addEventListener('DOMContentLoaded', init, false);
}
})();
// ==UserScript==
// @name nga.blocker
// @namespace http://ngacn.cc/
// @version 0.1
// @description Block threads by titles and authors
// @author OpenGG
// @match https://bbs.ngacn.cc/thread.php?fid=*
// @grant none
// ==/UserScript==
(() => {
'use strict';
const MatchWithArr = fn => (compare, arr) => {
for (let i = arr.length - 1; i > -1; --i) {
if (fn(compare, arr[i])) {
return true;
}
}
return false;
};
const random = Math.floor(
Math.random() * Math.pow(2, 53)
);
const clsHide = `_hydra_hidden_nga_${random}`;
const clsOptions = `_hydra_options_nga_${random}`;
const clsBtn = `_hydra_btn_nga_${random}`;
const css = `.${clsHide}{display:none}.${clsBtn}{position:absolute;top:100px;right:30px;opacity:0.6}.${clsBtn}:hover{opacity:1}.${clsOptions}{position:absolute;top:130px;right:30px;width:600px;padding:10px;background:#abc}.${clsOptions} label{display:block}`;
const addStyle = () => {
const style = document.createElement('style');
style.textContent = css;
(
document.head || document.documentElement
).appendChild(style);
};
const hideEl = el => {
el.classList.add(clsHide);
};
const blockedTopics = (
localStorage.blockedTopics ||
''
).split(/\r?\n/);
const blockedAuthors = (
localStorage.blockedAuthors ||
''
).split(/\r?\n/);
const block = () => {
const rows = document.querySelectorAll('.topicrow');
const matchAuthor = MatchWithArr((compare, item) => {
const matches = compare.match(/uid=(\d+)$/);
const uid = matches && matches[1];
return item === uid;
});
const matchTopic = MatchWithArr((compare, item) => {
return compare.indexOf(item) !== -1;
});
for (let i = rows.length - 1; i > -1; --i) {
const row = rows[i];
if (blockedAuthors.length > 0) {
const author = row.querySelector('a.author');
if (
matchAuthor(author.href, blockedAuthors)
) {
hideEl(row);
continue;
}
}
if (blockedAuthors.length > 0) {
const topic = row.querySelector('.topic');
if (
matchTopic(topic.textContent, blockedTopics)
) {
hideEl(row);
continue;
}
}
}
};
let optionsShow = false;
const hideOptions = () => {
optionsShow = false;
const el = document.querySelector(`.${clsOptions}`);
if (el) {
el.parentNode.removeChild(el);
}
};
const showOptions = () => {
optionsShow = true;
const container = document.createElement('div');
container.className = clsOptions;
container.innerHTML = '<label>屏蔽标题关键字(每行一个):<textarea placeholder="一行一个关键字"></textarea></label><label>屏蔽楼主UID(每行一个):<textarea placeholder="一行一个UID"></textarea></label><button>保存</button>';
const {
0: blockTopicEl,
1: blockAuthorEl
} = container.querySelectorAll('textarea');
const save = container.querySelector('button');
blockTopicEl.value = blockedTopics.join('\n');
blockAuthorEl.value = blockedAuthors.join('\n');
save.addEventListener('click', () => {
localStorage.blockedTopics = blockTopicEl.value;
localStorage.blockedAuthors = blockAuthorEl.value;
hideOptions();
location.reload();
}, false);
document.body.appendChild(container);
};
const addControls = () => {
const el = document.createElement('button');
el.className = clsBtn;
el.textContent = '屏蔽设置';
el.onclick = () => {
if (optionsShow) {
hideOptions();
} else {
showOptions();
}
};
document.body.appendChild(el);
};
const init = () => {
document.removeEventListener('DOMContentLoaded', init, false);
addStyle();
addControls();
block();
};
if (
document.readyState === 'interactive' ||
document.readyState === 'complete'
) {
init();
} else {
document.addEventListener('DOMContentLoaded', init, false);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment