Skip to content

Instantly share code, notes, and snippets.

View rdillmanCN's full-sized avatar

Richard Dillman rdillmanCN

  • Condé Nast
  • New York, NY.
View GitHub Profile
@rdillmanCN
rdillmanCN / throttle.js
Created December 30, 2017 09:27
Throttle function
/**
* Throttle functions.
*
* @param {Function} fn - The function to be throttled.
* @param {number} delay - The delay time in milliseconds.
* @param {Object|HTMLElement} scope - What this should be inside the function.
* @return {Function} - The throttled function wrapped with a new function.
*/
function throttle(fn, delay, scope) {
delay = delay || 250;
@rdillmanCN
rdillmanCN / isHidden.js
Created December 30, 2017 09:26
If the element were in the view-port, could we see it?
/**
* Is the element hidden from view? Here are the various ways this cold be.
* * Via css with opacity, display, or visibility.
* * Removed from the DOM, or not yet appended to the DOM
* * Within the overflow of a ancestor.
* * One of the methods above but applied to an ancestor element.
*
* @param {HTMLNode} el - element to examine
* @return {Boolean} Could the element be visible
*/
@rdillmanCN
rdillmanCN / isFriendlyIframe.js
Created December 30, 2017 09:25
Check if we have access to the iframe body.
/**
* Determines if the passed iframe is a friendly iframe.
*
* @param {HTMLElement} iframe The iframe under test.
* @return {boolean} True if friendly iframe, False otherwise.
*/
function isFriendlyIframe(iframe) {
var html = null;
try {
// deal with older browsers
@rdillmanCN
rdillmanCN / getAdData.js
Created December 30, 2017 09:23
This code will display page level targeting and slot level data including sizes, targeting, outOfPage, elementId, advertiserId, campaignId, creativeId, isBackfill, labelIds, lineItemId, outOfPage, sourceAgnosticCreativeId, sourceAgnosticLineItemId, and a URL to this creative at DFP.
/**
* Gathers the page targeting data.
*
* @private
* @param {Object} page - The internal object to append data to.
* @returns {undefined}
*/
function getPageTargeting(page) {
window.googletag.pubads().getTargetingKeys().forEach(function(keys) {
page.pageTargeting[keys] = window.googletag.pubads().getTargeting(keys);
@rdillmanCN
rdillmanCN / globals.js
Created December 30, 2017 09:21
Find all globals not native to the window object.
(function f() {
var frm = document.createElement("iframe");
frm.style.display = "none";
document.body.appendChild(frm);
var a = Object.getOwnPropertyNames(frm.contentWindow);
var b = Object.getOwnPropertyNames(window);
var tab = {};
b.filter(function(c) {
return a.indexOf(c) === -1;
}).map(function(i) {
@rdillmanCN
rdillmanCN / colors.js
Created December 30, 2017 09:20
Find all colors used on the page and out put to the console.
(function() {
var h = {};
var e = ["background-color", "color", "border-top-color", "border-right-color", "border-bottom-color", "border-left-color"];
var g = {
"rgb(0, 0, 0)": 1,
"rgba(0, 0, 0, 0)": 1,
"rgb(255, 255, 255)": 1
};
[].forEach.call(document.querySelectorAll("*"), function(i) {
var j = {};
@rdillmanCN
rdillmanCN / styles.js
Created December 30, 2017 09:19
List all style blocks and sheets used within the page, and output to the console.
(function() {
var tab = [].slice.call(document.styleSheets).map(function(e) {
return {
size: e.cssRules.length / 1e3 + " kb",
url: e.href || "Block"
}
});
console.group("CSS Data");
console.table(tab);
console.groupEnd("CSS Data");
@rdillmanCN
rdillmanCN / elementReady.js
Created December 30, 2017 09:19
Checks for an elements existence within a RAF within a promise.
/**
* Checks for an elements existence within a RAF within a promise.
*
* @param {string} selector - The element you wish to find. Defaults to 'body'.
* @param {string} target - The parent element to search within. Defaults to document.
* @return {Promise} Resolves when the element exists.
*/
function elementReady(selector, target) {
var options = {
@rdillmanCN
rdillmanCN / elementReady.js
Created December 30, 2017 03:35
Checks for an elements existence within a RAF within a promise.
/**
* Checks for an elements existence within a RAF within a promise.
*
* @param {string} selector - The element you wish to find. Defaults to 'body'.
* @param {string} target - The parent element to search within. Defaults to document.
* @return {Promise} Resolves when the element exists.
*/
function elementReady(selector, target) {
var options = {
target: target || document,
@rdillmanCN
rdillmanCN / styles.js
Created December 5, 2017 21:54
List all style blocks and sheets used within the page, and output to the console.
(function() {
var tab = [].slice.call(document.styleSheets).map(function(e) {
return {
size: e.cssRules.length / 1e3 + " kb",
url: e.href || "Block"
}
});
console.group("CSS Data");
console.table(tab);
console.groupEnd("CSS Data");