Skip to content

Instantly share code, notes, and snippets.

View kentbrew's full-sized avatar

Kent Brewster kentbrew

View GitHub Profile
@kentbrew
kentbrew / _index.md
Last active December 30, 2017 00:07
Toggling Global Features in Browser Extensions

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 June 16, 2017 21:20
Some URLs Abbreviated in Chrome When Processed By JavaScript

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 June 12, 2017 21:47
XHR + Promises + responseType
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 June 21, 2017 16:42
Firefox Won't Open a New Tab from an Iframe in a Web Extensions Browser Extension
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);
@kentbrew
kentbrew / pinterest_meta.md
Created June 5, 2017 22:12
Pinterest's META Namespace

Just like Facebook and Twitter, Pinterest has its own namespace. Go like this:

<meta property="pin:url" content="https://yourpage.com/pinit/test/pinterest_meta.html" />
<meta property="pin:description" content="This pin was generated by the Pinterest meta." />
<meta property="pin:media" content="http://cdn.yourpage.com/your_image_here.jpg" />

... and your preferred image (which need not actually appear on the page) will show at the top of the grid when someone uses Pinterest's mobile device, browser extension, or Any Image Save button.

@kentbrew
kentbrew / IPBSIEIP.md
Created February 17, 2017 23:29
Implementing the President's Border Security and Immigration Enforcement Improvements Policies

January 25, 2017

MEMORANDUM FOR:

Kevin McAleenan, Acting Commissioner, U.S. Customs and Border Protection

Dan Ragsdale, Acting Director, U.S. Immigration and Customs Enforcement

Lori Scialabba, Acting Director, U.S. Citizenship and Immigration Services

@kentbrew
kentbrew / stub.html
Last active June 9, 2020 21:58
A stub file for new HTML projects.
<!doctype html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<link rel="stylesheet" href="presentation.css">
</head>
<body>
<script src="behavior.js"></script>
</body>
@kentbrew
kentbrew / WebExtensionCheck.js
Last active August 6, 2018 14:37
How to tell if visitors to your site are running a WebExtensions-compatible extension
// If we know the extension's ID and a path to one of its web-accessible resources, we can quietly check for presence.
(function (w, a) {
var checkExt = function (callback) {
var hazExt = false;
var img = new Image();
img.onload = function () {
hazExt = true;
};
// need to know the right protocol for Firefox and Edge extensions
@kentbrew
kentbrew / spinner.svg
Last active February 26, 2017 17:49
An animated SVG, suitable for use as a progress indicator.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@kentbrew
kentbrew / webExCompat.txt
Last active November 18, 2016 00:14
How to tell if your Chrome browser extension has WebExtensions support
From the root directory of your Chrome extension:
grep -r " chrome\." . | sed s/.*chrome\./\"/g | sed s/\(.*/#Browser_compatibility/ | sed s/[.]/\\//g | sed s/./https:\\/\\/developer\.mozilla\.org\\/Add-ons\\/WebExtensions\\/API\\// | sort | uniq
Hopefully you'll get something like this:
https://developer.mozilla.org/Add-ons/WebExtensions/API/i18n/getMessage#Browser_compatibility
https://developer.mozilla.org/Add-ons/WebExtensions/API/runtime/sendMessage#Browser_compatibility
https://developer.mozilla.org/Add-ons/WebExtensions/API/storage/local/get#Browser_compatibility
https://developer.mozilla.org/Add-ons/WebExtensions/API/storage/local/set#Browser_compatibility