Skip to content

Instantly share code, notes, and snippets.

@daniellizik
Created August 18, 2015 12:54
Show Gist options
  • Save daniellizik/15cd71e16777ff40667e to your computer and use it in GitHub Desktop.
Save daniellizik/15cd71e16777ff40667e to your computer and use it in GitHub Desktop.
random utilities
/** creates overlay and iframe module via bookmarklet */
(function() {
var args = Array.prototype.slice.call(arguments);
var iframeId = args.filter(function(a){return a.el === "IFRAME"})[0].id;
var overlayId = args.filter(function(a){return a.el === "div"})[0].id;
args.forEach(function(obj) {
var el = document.createElement(obj.el);
el.id = obj.id;
if (obj.src) el.src = obj.src;
for (var p in obj.style) el.style[p] = obj.style[p];
(document.body||document.documentElement).appendChild(el);
});
window.addEventListener("click", function(e) {
if (e.target.id !== iframeId) {
var frameEl = document.getElementById(iframeId);
var overlayEl = document.getElementById(overlayId);
if (!frameEl || !overlayEl) return;
frameEl.parentNode.removeChild(frameEl);
overlayEl.parentNode.removeChild(overlayEl);
}
});
})({
el: "div",
id: "overlay",
style: {
backgroundColor: "#A4A4A4",
opacity: ".6",
position: "fixed",
left: "0",
top: "0",
height: "100%",
width: "100%"
}
}, {
el: "IFRAME",
src: "http://localhost/...",
id: "dbiWrap",
style: {
width: "30%",
height: "50%",
zIndex: "9999999",
position: "fixed",
top: "25%",
left: "35%",
border: "none"
}
});
/** inject script into dom via chrome extension */
function contentScriptInjection (arr, type) {
arr.map(function(obj) {
obj.names.map(function(script) {
var elType = type === "script"? "script" : "link",
sourceProp = type === "script" ? "src" : type === "style" ? "href" : false,
ext = type === "script" ? ".js" : type === "style" ? ".css" : false,
sourceType = type === "script" ? "text/javascript" : type === "style" ? "text/style" : false,
typeProp = type === "script" ? "type" : type === "style" ? "rel" : false,
el = document.createElement(elType);
el[sourceProp] = chrome.extension.getURL(obj.path+script+ext);
el[typeProp] = sourceType;
(document.body||document.documentElement).appendChild(el);
});
});
}
/** upload local scripts to document head */
function load(scripts) { //takes array of scripts without .js extension, they must be in local file
for (var i = 0; i < scripts.length; i++) {
var script = document.createElement("script");
script.src = scripts[i]+'.js';
document.head.appendChild(script);
script.onload = i == scripts.length - 1 ? execute : false;
}
};
/** append to DOM */
function append(obj) {
var child = document.createElement(obj.el);
var parent = obj.p == document.body ? document.body : document.getElementById(obj.p);
for (var p in obj) {
child[p] = obj[p];
}
parent.appendChild(child);
};
/** JSON.stringify style for more "elaborate" window alerts */
var jsonStyle = JSON.stringify(data, null, 3).replace(/[\]\[\{\}\,\\"]*/g, "");
/** leading zeros
* @input {string or integer}
* @zeros (integer)
* @mode (optional) {boolean}: true = appends zeros equal to 2nd parameter
*/
function leadingZeros(input, zeros, mode) {
var string = input.toString();
var difference = zeros - string.length > 0 ? zeros - string.length : 0;
var set = typeof input === "number" ? string.split('') : input.split('');
mode = mode === true ? zeros : difference;
for (var i = 0; i < mode; i++) set.splice(0, 0, "0");
return set.join('')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment