Skip to content

Instantly share code, notes, and snippets.

@dvingerh
Created March 10, 2018 15:06
Show Gist options
  • Select an option

  • Save dvingerh/2d4b30e687e8dd522739494901f6469d to your computer and use it in GitHub Desktop.

Select an option

Save dvingerh/2d4b30e687e8dd522739494901f6469d to your computer and use it in GitHub Desktop.
//META{"name":"HideBlockedUsers"}*//
class HideBlockedUsers {
constructor () {
this.messageObserver = new MutationObserver(() => {});
}
getName () {return "HideBlockedUsers";}
getDescription () {return "Completely hide messages from blocked users.";}
getVersion () {return "1.0.0";}
getAuthor () {return "Cammy";}
//legacy
load () {}
start () {
var jqueryScr = document.createElement("script");
jqueryScr.type = "text/javascript";
jqueryScr.src = "https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js";
document.head.appendChild(jqueryScr);
var initScr = document.createElement("script");
initScr.type = "text/javascript";
initScr.src = "https://cdn.rawgit.com/pie6k/jquery.initialize/master/jquery.initialize.js";
document.head.appendChild(initScr);
this.main();
}
main () {
;(function ($) {
"use strict";
var combinators = [' ', '>', '+', '~']; // https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors#Combinators
var fraternisers = ['+', '~']; // These combinators involve siblings.
var complexTypes = ['ATTR', 'PSEUDO', 'ID', 'CLASS']; // These selectors are based upon attributes.
// Understand what kind of selector the initializer is based upon.
function grok(msobserver) {
if (!$.find.tokenize) {
// This is an old version of jQuery, so cannot parse the selector.
// Therefore we must assume the worst case scenario. That is, that
// this is a complicated selector. This feature was available in:
// https://github.com/jquery/sizzle/issues/242
msobserver.isCombinatorial = true;
msobserver.isFraternal = true;
msobserver.isComplex = true;
return;
}
// Parse the selector.
msobserver.isCombinatorial = false;
msobserver.isFraternal = false;
msobserver.isComplex = false;
var token = $.find.tokenize(msobserver.selector);
for (var i = 0; i < token.length; i++) {
for (var j = 0; j < token[i].length; j++) {
if (combinators.indexOf(token[i][j].type) != -1)
msobserver.isCombinatorial = true; // This selector uses combinators.
if (fraternisers.indexOf(token[i][j].type) != -1)
msobserver.isFraternal = true; // This selector uses sibling combinators.
if (complexTypes.indexOf(token[i][j].type) != -1)
msobserver.isComplex = true; // This selector is based on attributes.
}
}
}
// MutationSelectorObserver represents a selector and it's associated initialization callback.
var MutationSelectorObserver = function (selector, callback, options) {
this.selector = selector.trim();
this.callback = callback;
this.options = options;
grok(this);
};
// List of MutationSelectorObservers.
var msobservers = [];
msobservers.initialize = function (selector, callback, options) {
// Wrap the callback so that we can ensure that it is only
// called once per element.
var seen = [];
var callbackOnce = function () {
if (seen.indexOf(this) == -1) {
seen.push(this);
$(this).each(callback);
}
};
// See if the selector matches any elements already on the page.
$(options.target).find(selector).each(callbackOnce);
// Then, add it to the list of selector observers.
var msobserver = new MutationSelectorObserver(selector, callbackOnce, options)
this.push(msobserver);
// The MutationObserver watches for when new elements are added to the DOM.
var observer = new MutationObserver(function (mutations) {
var matches = [];
function push(match) {
matches.push(match);
}
// For each mutation.
for (var m = 0; m < mutations.length; m++) {
// If this is an attributes mutation, then the target is the node upon which the mutation occurred.
if (mutations[m].type == 'attributes') {
// Check if the mutated node matchs.
if (mutations[m].target.matches(msobserver.selector))
matches.push(mutations[m].target);
// If the selector is fraternal, query siblings of the mutated node for matches.
if (msobserver.isFraternal)
mutations[m].target.parentElement.querySelectorAll(msobserver.selector).forEach(push);
else
mutations[m].target.querySelectorAll(msobserver.selector).forEach(push);
}
// If this is an childList mutation, then inspect added nodes.
if (mutations[m].type == 'childList') {
// Search added nodes for matching selectors.
for (var n = 0; n < mutations[m].addedNodes.length; n++) {
if (!(mutations[m].addedNodes[n] instanceof Element)) continue;
// Check if the added node matches the selector
if (mutations[m].addedNodes[n].matches(msobserver.selector))
matches.push(mutations[m].addedNodes[n]);
// If the selector is fraternal, query siblings for matches.
if (msobserver.isFraternal)
mutations[m].addedNodes[n].parentElement.querySelectorAll(msobserver.selector).forEach(push);
else
mutations[m].addedNodes[n].querySelectorAll(msobserver.selector).forEach(push);
}
}
}
// For each match, call the callback using jQuery.each() to initialize the element (once only.)
matches.forEach(function(match) {
$(match).each(msobserver.callback);
});
});
// Observe the target element.
var defaultObeserverOpts = { childList: true, subtree: true, attributes: msobserver.isComplex };
observer.observe(options.target, options.observer || defaultObeserverOpts );
};
// Deprecated API (does not work with jQuery >= 3.1.1):
$.fn.initialize = function (callback, options) {
msobservers.initialize(this.selector, callback, $.extend({}, $.initialize.defaults, options));
};
// Supported API
$.initialize = function (selector, callback, options) {
msobservers.initialize(selector, callback, $.extend({}, $.initialize.defaults, options));
};
// Options
$.initialize.defaults = {
target: document.documentElement, // Defaults to observe the entire document.
observer: null // MutationObserverInit: Defaults to internal configuration if not provided.
}
})(jQuery);
$(".message-group-blocked").initialize(function() {
$(this).hide();
$(this).prev().removeClass("has-divider");
console.info("Message block and divider removed.");
})
}
stop () {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment