Skip to content

Instantly share code, notes, and snippets.

Robert Knight robertknight

Block or report user

Report or block robertknight

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
robertknight /
Last active Oct 15, 2019
Using Hypothesis in a JavaScript SPA with client-side routing

This is a sample which demonstrates how to use Hypothesis <= v0.21.0 in a JavaScript Single Page Application which does client-side routing.

The current version of Hypothesis at the time of writing does not automatically update the loaded annotations when the page URL changes. This sample takes the heavy-handed approach of completely removing and reloading Hypothesis when the URL changes. In future we'll look to build this into Hypothesis automatically in a much more performant way.

Running the demo

  1. Download files from this gist and extract into a directory
  2. Run python -m SimpleHTTPServer 8000
View jscosine.js
function termFreqMap(str) {
var words = str.split(' ');
var termFreq = {};
words.forEach(function(w) {
termFreq[w] = (termFreq[w] || 0) + 1;
return termFreq;
function addKeysToDict(map, dict) {
robertknight /
Created Oct 3, 2019
Remove __future__ imports from Python files
import sys
files = sys.argv[1:]
for path in files:
print(f"Updating {path}")
out_lines = []
prev_line_was_future_import = False
with open(path) as file:
robertknight /
Created Jul 3, 2017
Testing the Windows screenreader NVDA on a Mac

How to test NVDA screen reader behaviour on a Mac:

  1. Download Microsoft Edge VM from
  2. Download Virtualbox and import the Edge VM image.

Then in the VM:

  1. Install guest addons in the VM
  2. Download & install latest NVDA from
  3. Download & install SharpKeys and use it to map left an alternative key (eg. Left Ctrl) to the Insert key. This is needed because Macs do not typically have an “Insert” key which is the prefix for many NVDA commands.
robertknight /
Last active Sep 12, 2019
Chrome range request + HTTP cache interaction issue

Steps to reproduce:

  1. Save range-request.html locally and open it in Chrome
  2. Clear your browser cache by going to chrome://settings/clearBrowserData, checking "Cached images and files" and clicking "Clear data"
  3. Open the devtools and make sure that the "Disable cache" option is not checked in the Network tab.
  4. Click the "Fetch file with Range requests" button. Observe the console logs and wait for the complete file to be fetched.
  5. Click the "Fetch file with one request" button and observe the console logs.
  6. Click the "Fetch file with one request" button again and observe the console logs.

Expected result:

robertknight / google-drive-test.diff
Created Aug 22, 2019
Cross-site file fetching test in the Hypothesis LMS app
View google-drive-test.diff
diff --git a/lms/static/scripts/frontend_apps/components/FilePickerApp.js b/lms/static/scripts/frontend_apps/components/FilePickerApp.js
index 1e32c81..a040a75 100644
--- a/lms/static/scripts/frontend_apps/components/FilePickerApp.js
+++ b/lms/static/scripts/frontend_apps/components/FilePickerApp.js
@@ -91,9 +91,11 @@ export default function FilePickerApp({
try {
const { id, url } = await googlePicker.showPicker();
+ await googlePicker.fetchFile(id);
await googlePicker.enablePublicViewing(id);
robertknight /
Last active Aug 21, 2019
How Istanbul works

Istanbul Notes

These are some notes I made while reviewing hypothesis/client#156 to understand how Istanbul works

Istanbul instruments code in order to generate code coverage metrics for tests by adding code to record lines, statements etc. that are executed.

It adds a global __coverage__ variable to the generated code which is a map from file path to coverage information. The code for each module is then augmented with:

robertknight / delay.diff
Created Jul 29, 2019
Add a short delay to action dispatches
View delay.diff
diff --git a/src/sidebar/store/index.js b/src/sidebar/store/index.js
index 8051a738..1e725db5 100644
--- a/src/sidebar/store/index.js
+++ b/src/sidebar/store/index.js
@@ -71,6 +71,33 @@ function angularDigestMiddleware($rootScope) {
+ * Middleware which adds an artificial delay to all actions going through
robertknight /
Last active Jul 7, 2019
Selection change event notes

Notes on selection events in mobile browsers (April 2016)

Notes on events that are fired during text selection in current desktop and mobile browsers.

Testing steps:

  1. Serve test.html from local dev server and open on device
  2. Make an initial selection via long-press on the text, observing reported events
  3. Modify the selection using drag handles and observe reported events
robertknight /
Last active May 28, 2019
Minimal Webpack DllPlugin example

Compile with:

webpack --config vendor.webpack.config.js
webpack --config app.webpack.config.js

Use with the following index.html

You can’t perform that action at this time.