Skip to content

Instantly share code, notes, and snippets.

Kent Brewster kentbrew

Block or report user

Report or block kentbrew

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@kentbrew
kentbrew / background.js
Last active Nov 30, 2017
Tracker Jacker
View background.js
const BLOCK_ME = "://www.facebook.com/tr/";
chrome.webRequest.onBeforeRequest.addListener(
function(r) {
return {
cancel: r.url.indexOf(BLOCK_ME) != -1
};
},
{urls: ["<all_urls>"]},
["blocking"]
@kentbrew
kentbrew / DOMParser_input_cleaner.md
Last active Oct 24, 2017
Using DOMParser to clean HTML input
View DOMParser_input_cleaner.md

Old School:

  var clean = function (input) {
    var testMe = input, dupeTest = '';
    while (testMe !== dupeTest) {
      testMe = new DOMParser().parseFromString(testMe, "text/html").documentElement.textContent;
      dupeTest = testMe;
    }
    testMe = testMe.replace(/</g, '&lt;');
    return testMe;
@kentbrew
kentbrew / readme.md
Created Jul 11, 2017
Some Notes On Zetterberg's Static Search Widget
View readme.md
@kentbrew
kentbrew / wilensky.js
Created Jul 9, 2017
Imagine a room full of 100 people with 100 dollars each. With every tick of the clock, every person with money gives a dollar to one randomly chosen other person. After some time progresses, how will the money be distributed?
View wilensky.js
// paste me into console to explore the counterintuitive problem found here:
// http://www.decisionsciencenews.com/2017/06/19/counterintuitive-problem-everyone-room-keeps-giving-dollars-random-others-youll-never-guess-happens-next/
// setup: tweak as needed
var rounds = 10, turns = 1000, max = 100, start = 100;
// play one round
var round = function () {
// setup
@kentbrew
kentbrew / behavior.js
Created Jun 23, 2017
Scale and Render an Image for Visual Search
View behavior.js
(function (w, d, a) {
var $ = w[a.k] = {
"a": a, "w": w, "d": d,
"s": {},
"v": {},
"f": (function () {
return {
// get a DOM property or text attribute
get: function (el, att) {
var v = null;
@kentbrew
kentbrew / readme.md
Last active Sep 11, 2017
Towards A Modernized JavaScript Class Name Changer
View readme.md

Towards A Modernized JavaScript Class Name Changer

Previously we'd do something horrible with string matching when we wanted to change an HTML element's class name with JavaScript.

var changeClass = function (el, add, remove) {
  if (el) {

    if (!el.className) {
      el.className = '';
@kentbrew
kentbrew / _index.md
Last active Dec 30, 2017
Toggling Global Features in Browser Extensions
View _index.md

Toggling Global Features in Browser Extensions

Developers wanting to add context menus to browser extensions face an all-or-nothing situation: context menu items, being global features, will be present on all pages once created. Adding a context menu item is easy, but if we want it to show only on certain Internet domains we will need to:

  • keep an eye on the active tab
  • check its domain whenever it changes
  • show or hide the context menu depending on the domain of the page in the tab

APIs we'll be using:

@kentbrew
kentbrew / wat_url.md
Created Jun 16, 2017
Some URLs Abbreviated in Chrome When Processed By JavaScript
View wat_url.md

Some URLs Abbreviated in Chrome When Processed By JavaScript

While rebuilding the Pinterest extension for Chrome we ran across a weird edge case at vox.com. We could preview their nice big fat srcset-enabled images in the grid and scrape + post to Pinterest, but could not take the src attribute of an IMG tag and directly apply it to the background-image style attribute of a new DIV.

Here are some reduced cases:

Win

Copy this to clipboard:

@kentbrew
kentbrew / xhr_promises.js
Created Jun 12, 2017
XHR + Promises + responseType
View xhr_promises.js
var xhr = o => {
return new Promise(function(resolve, reject) {
var req = new XMLHttpRequest();
o.method = o.method || 'GET';
// third parameter must be there and be true to use o.formData
req.open(o.method, o.url, true);
// default text; can send json
req.responseType = o.responseType || 'text';
// get reply back from API in proper language
@kentbrew
kentbrew / content.js
Last active Jun 21, 2017
Firefox Won't Open a New Tab from an Iframe in a Web Extensions Browser Extension
View content.js
var browser = chrome || browser;
var iframe = document.createElement('IFRAME');
iframe.style.position = 'fixed';
iframe.style.height = '100px';
iframe.style.width = '300px';
iframe.style.top = '0';
iframe.style.right = '0';
iframe.src = browser.extension.getURL('iframe.html');
document.body.appendChild(iframe);
You can’t perform that action at this time.