Skip to content

Instantly share code, notes, and snippets.

@guangtuan
Created May 28, 2018 04:14
Show Gist options
  • Save guangtuan/6725f1e04232d00a0ffab8a31ffbd825 to your computer and use it in GitHub Desktop.
Save guangtuan/6725f1e04232d00a0ffab8a31ffbd825 to your computer and use it in GitHub Desktop.
github moments helper
// ==UserScript==
// @name gayhub moments helper
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://github.com/
// @grant none
// ==/UserScript==
const TYPES = {
FORK: 'fork',
FOLLOW: 'follow',
WATCH_STARTED: 'watch_started'
};
const rules = {
// linpingchuan: Object.values(TYPES)
};
const debug = true;
const print = (...args) => {
if (debug) {
console.log.apply(null, args);
}
};
const findContainer = () => {
const parentClass = 'news';
return document.getElementsByClassName(parentClass)[0];
};
const listenMomentsSeeded = (parent, cb) => {
var config = { childList: true };
var observer;
var callback = mutationsList => {
let typeMatch = ({ type }) => type === 'childList';
let target = mutationsList.find(typeMatch);
if (target === null || target === undefined) {
return;
}
let seedNumber = target.addedNodes.length;
print(`${seedNumber} child nodes append`);
if (seedNumber > 1) {
cb && cb();
print('true moments content seeded');
}
};
observer = new MutationObserver(callback);
observer.observe(parent, config);
};
const matchRules = div => {
const USER_TAG_IN_MOMENT_ITEM =
'div.d-flex.flex-items-baseline > div > a.link-gray-dark.no-underline.text-bold.wb-break-all';
let tag = div.querySelector(USER_TAG_IN_MOMENT_ITEM);
if (!!tag) {
let types = rules[tag.innerHTML];
if (!!types) {
return arrayCross(div.className.split(' '), types);
}
}
return false;
};
const arrayCross = (arr1, arr2) => {
for (let element of arr1) {
if (arr2.includes(element)) {
return true;
}
}
return false;
};
const applyRules = container => () => {
const deleteSelf = node => node.remove();
[].concat(...container.children).filter(matchRules).forEach(deleteSelf);
};
(function () {
'use strict';
const container = findContainer();
listenMomentsSeeded(container, applyRules(container));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment