Skip to content

Instantly share code, notes, and snippets.

@xy0
Last active October 4, 2017 20:50
Show Gist options
  • Save xy0/de2024f046eb9eb767c7404016a34bcb to your computer and use it in GitHub Desktop.
Save xy0/de2024f046eb9eb767c7404016a34bcb to your computer and use it in GitHub Desktop.
User Script Template
// ==UserScript== .
// @name XY0-defaultUserScript
// @namespace MESH.FYN.XY0.UScript
// @description F
// @include *
// @version 0.1
// @grant none
// ==/UserScript== .
console.log(' \n' ,
' \\_|\\ \n' ,
' ---script--- \n');
/////////////////////////\\\\\\\\\\\\\\\\50\|
//
// { }
//
// CONFIG
// SubScripts
// ( scripts that match certain URL patterns )
var subScripts = [
{
name: 'v4c',
match: 'cytu.be',
code: function() {
var enlargeSkipButton = function() {
addCSSRule(sheet, "#voteskip", "z-index: 9000;");
addCSSRule(sheet, "#voteskip", "position: fixed");
addCSSRule(sheet, "#voteskip", "right: 20%");
addCSSRule(sheet, "#voteskip", "bottom: 10%");
addCSSRule(sheet, "#voteskip", "color: black");
addCSSRule(sheet, "#voteskip", "width: 300px");
addCSSRule(sheet, "#voteskip", "height: 300px");
addCSSRule(sheet, "#voteskip", "border-radius: 50%");
addCSSRule(sheet, "#voteskip", "border: 5px solid");
};
$(function() {
console.log("- DOM ready");
});
$('#cinematoggle').click( function() {
console.log("- Enlarging Skip Button");
enlargeSkipButton();
});
/* end code */
}
}
];
//////////////////////////////////////////////
//
// BUILT-IN
// FUNCTIONS
function cl(string) {
console.log('~s ' + string);
}
function cw(string) {
console.warn('~s ' + string);
}
function ce(string) {
console.error('~s ' + string);
}
// choose a script from a list of page url fragments
//
var pickScript = function(scripts) {
var pageURL = window.location.href;
cl( 'pageUrl:' + pageURL );
for( var index in scripts ) {
if( pageURL.indexOf(scripts[index].match) !== -1 ) {
cl( 'running: ' + scripts[index].name );
return scripts[index];
}
}
};
// passes the pageQuaities param to the script to be ran
//
var runScripts = function(pageQualities) {
var script = pickScript(subScripts);
for( var dependency in script.dependencies ) {
if( pageQualities[dependency] === false ) {
ce('Dependency not met for subScript');
}
}
runScript(script);
};
// runs a given script from a supplied array of scripts
// and a script to pick
var runScript = function(script) {
script.code();
};
// creates custom CSS rules
// impliment: addCSSRule(sheet, "*", "color: black");
var sheet = (function() {
cl("Adding new Style Sheet");
// Create the <style> tag
var style = document.createElement("style");
// Add a media (and/or media query) here if you'd like!
// style.setAttribute("media", "screen")
// style.setAttribute("media", "only screen and (max-width : 1024px)")
// WebKit hack :(
style.appendChild(document.createTextNode(""));
// Add the <style> element to the page
document.head.appendChild(style);
return style.sheet;
})();
// adds the above CSS rule to the document in realtime
// impliment: addCSSRule(sheet, "*", "color: black");
function addCSSRule(sheet, selector, rules, index) {
cl("Adding Rule to Style Sheet");
if("insertRule" in sheet) {
sheet.insertRule(selector + "{" + rules + "}", index);
}
else if("addRule" in sheet) {
sheet.addRule(selector, rules, index);
}
}
// generates random hex color values
//
function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
// below is a custom wait until exists function
;(function ($, window) {
var intervals = {};
var removeListener = function(selector) {
if (intervals[selector]) {
window.clearInterval(intervals[selector]);
intervals[selector] = null;
}
};
var found = 'waitUntilExists.found';
$.fn.waitUntilExists = function(handler, shouldRunHandlerOnce, isChild) {
var selector = this.selector;
var $this = $(selector);
var $elements = $this.not(function() { return $(this).data(found); });
if (handler === 'remove') {
// Hijack and remove interval immediately if the code requests
removeListener(selector);
} else {
// Run the handler on all found elements and mark as found
$elements.each(handler).data(found, true);
if (shouldRunHandlerOnce && $this.length) {
// Element was found, implying the handler already ran for all
// matched elements
removeListener(selector);
} else if (!isChild) {
// If this is a recurring search or if the target has not yet been
// found, create an interval to continue searching for the target
intervals[selector] = window.setInterval(function () {
$this.waitUntilExists(handler, shouldRunHandlerOnce, true);
}, 500);
}
}
return $this;
};
}(jQuery, window));
// adding remove to traditional array prototype
// will return number of removed elements, 0 otherise
// if true is provided, the script will return the array
Array.prototype.remove = function(elem, returnVar) {
for( var i=0; i<this.length; i++ ) {
console.log("wewlad");
var result = 0;
if(this[i] == elem) {
this.splice(i, 1);
result = result + 1;
}
if( i >= this.length - 1) {
if(returnVar == true) {
return elem;
} else {
return result;
}
}
}
};
//////////////////////////////////////////////
//
// Tests &
// INIT
var pageQualities = {
jQuery: null,
};
// run main() and subScripts
// but also test to see if jQuery is defined
if (typeof $ === 'undefined' || !$) {
// test to see if jQuery object exists (not $)
if (typeof jQuery === 'undefined' || !jQuery) {
cw('jQuery not defined');
pageQualities.jQuery = false;
runScripts(pageQualities);
} else {
var $ = jQuery;
pageQualities.jQuery = true;
runScripts(pageQualities);
}
} else {
pageQualities.jQuery = true;
runScripts( pageQualities );
}
function main() {
//////////////////////////////////////////////
//
// Main Code
// ( run on every page )
}
console.log(' --/script--- ');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment