Skip to content

Instantly share code, notes, and snippets.

@mikedfunk
Forked from jr-codes/script.user.js
Last active August 29, 2015 14:11
Show Gist options
  • Save mikedfunk/48d6524ba4f9d2a4baf9 to your computer and use it in GitHub Desktop.
Save mikedfunk/48d6524ba4f9d2a4baf9 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Userscript Name
// @namespace http://example.com/
// @description Userscript Description
// @match http://example.com/* (or @include * to include all pages)
// @version 1.0
// ==/UserScript==
// Inject JavaScript onto the page
function exec(fn) {
var script = document.createElement('script');
script.textContent = '(' + fn + ')();';
document.head.appendChild(script);
}
// Include external JS/CSS file onto the page
function include(url, type) {
type = (type || url.split('.').pop()).toLowerCase();
if (type === 'css') {
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = url;
document.head.appendChild(link);
} else if (type === 'js') {
var script = document.createElement('script');
script.src = url;
script.async = false;
document.head.appendChild(script);
} else {
throw new Error('Failed to include ' + url + ' due to unknown file type.');
}
}​
// Executes function asynchronously
function async(fn) {
setTimeout(fn, 0);
}
// include jquery if it doesn't already exist
if (typeof jQuery == 'undefined') {
include('https://code.jquery.com/jquery-2.1.3.min.js', 'js');
}
// include javascript or css here...
// include('https://rawgit.com/username/project/master/file.js', 'js');
// include('https://rawgit.com/username/project/master/file.css', 'css');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment