Skip to content

Instantly share code, notes, and snippets.

View JamesMGreene's full-sized avatar

James M. Greene JamesMGreene

View GitHub Profile
var btn = document.getElementById("copy-button");
btn.addEventListener("click", clickHandler, false);
function clickHandler(e) {
var clip = new ClipboardEvent("copy");
clip.clipboardData.setData("text/plain", "foo");
clip.clipboardData.setData("text/html", "<b>foo</b>");
// CRITICAL: Must call `preventDefault();` to get this data into the system/desktop clipboard!!!
clip.preventDefault();
@JamesMGreene
JamesMGreene / example_def.rb
Last active September 15, 2018 09:00
Ruby class methods vs. instance methods
class Human
# Class method (a.k.a. static method)
def self.classification
'Mammal'
end
# Instance constructor
def initialize(first_name, last_name)
@JamesMGreene
JamesMGreene / phantomjs-xhr.js
Last active June 11, 2018 10:53
Example of (a) successfully executing a cross-domain XHR from the PhantomJS outer context to a wildcard CORS-enabled site; and (b) failing to execute a cross-domain XHR from the PhantomJS outer context to a site that is not CORS-enabled.
var urls = [
/* Wildcard CORS enabled - Works in PhantomJS 1.9.0 */
'http://updates.html5rocks.com',
/* CORS disabled - Fails in PhantomJS 1.9.0 (and every other version) */
'http://www.google.com',
/* Hack workaround? */
/*
function(httpGet, callback) {
phantom.page.settings = (phantom.page.settings || {});
phantom.page.settings.webSecurityEnabled = false;
@JamesMGreene
JamesMGreene / 0 - README.md
Last active February 20, 2018 18:10
A brief example of the latest idea for communicating with PhantomJS from the client-side.

A brief example of the latest idea for communicating with PhantomJS from the client-side.

This is the next revision of ideas discussed in a previous Gist:
    https://gist.github.com/JamesMGreene/3716654

This example demonstrates shimming in the new communication channels under discussion via existing methods (WebPage#onCallback and window.callPhantom).

The example does, however, take advantage of a yet-to-be-implemented EventEmitter API for core modules in the PhantomJS outer context. This API would add the following methods to core modules as appropriate:

  • on / addEventListener
@JamesMGreene
JamesMGreene / 0 - README.md
Created September 13, 2012 18:48
PhantomJS: Conceptual implementation of a short-term shim to establish the window.postMessage API for IPC

Topic

Providing an inter-process communication (IPC) mechanism such that a WebPage can explicitly signal back to PhantomJS as a push, thus eliminating/minimizing the need for users to setup polling functions, etc.

In PhantomJS 1.6, @detro added a WebPage onCallback handler that could be triggered from a webpage client by invoking the specially attached window.callPhantom function. However, @ariya expressed some discontent with this approach and so the three of us began discussing utilizing an existing API for cross-domain messaging instead: window.postMessage. (See Discussion for more info.)

Problems

PhantomJS currently only allows for a single handler per signal (a separate problem I'm working on over here). As such, automatically attaching internal handler may prevent users from attaching their own handlers for onInitialized (tha

@JamesMGreene
JamesMGreene / findText.js
Created January 13, 2014 14:49
Showing Phantom supporting searching for text using the normal browser DOM APIs.
var page = require("webpage").create();
page.onConsoleMessage = function(msg) {
console.log('[PAGE] Message: ' + msg);
};
page.open("http://google.com/", function(status) {
if (status !== "success") {
console.error("Failed to load the page. Usually this means some resource failed to load.");
phantom.exit(1);
@JamesMGreene
JamesMGreene / README.md
Last active February 6, 2018 05:06
Trying out nexe@2.x beta native module bundling

Environment

  • Windows 10 (x64)
  • VS2015
  • Python 2.7.11
  • node@6.11.0
  • nexe@2.0.0-beta.2
  • fuse-box@2.2.1
@JamesMGreene
JamesMGreene / genRandomStrings.js
Created December 12, 2017 17:44
Generic random alphabetical strings with JavaScript
var validChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
var validCharsUpperBound = validChars.length - 1;
function randomInt(lower, upper) {
return lower + Math.floor(Math.random() * (upper - lower + 1))
}
function randomLetter() {
return validChars[randomInt(0, validCharsUpperBound)];
}
@JamesMGreene
JamesMGreene / gist:ec88338f047db436a1865aae23af83fd
Created November 17, 2017 15:02 — forked from kitek/gist:1579117
NodeJS create md5 hash from string
var data = "do shash'owania";
var crypto = require('crypto');
crypto.createHash('md5').update(data).digest("hex");
This file has been truncated, but you can view the full file.
(function(FuseBox){FuseBox.$fuse$=FuseBox;
FuseBox.pkg("default", {}, function(___scope___){
___scope___.file("index.js", function(exports, require, module, __filename, __dirname){
var zmq = require('zmq')
var pub = zmq.socket('pub')
pub.bindSync('tcp://127.0.0.1:3000')
console.log('Publisher bound to port 3000')