Skip to content

Instantly share code, notes, and snippets.

@plantvsbirds
Created February 14, 2019 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plantvsbirds/646b4f067739d62366303013720d3d0b to your computer and use it in GitHub Desktop.
Save plantvsbirds/646b4f067739d62366303013720d3d0b to your computer and use it in GitHub Desktop.
hey, here's a bolt, and some metal parts, would you like to put them together?
wrap2 = (c) => {
let isPropFunc = (k) => (typeof c[k] === 'function')
let isProto = (k) => isPropFunc(k) && !!c[k].prototype
let logAccess = (k, v) => {
console.log(`trying to access ${k} at ${c} with ${v}`);
}
console.log(`wrapping ${c}`)
let res = {}
let shouldBind = (v) => !!v && (typeof v == 'object') && (!v.prototype) && (v.__proto__ === HTMLDocument.prototype || v.__proto__ === Window.prototype || v.prototype && v.prototype.isPrototypeOf(document.HTMLElementPrototype))
let cat = (k) => (...args) => {
let v = null
try {
v = c[k].apply(c, args);
} catch (e) {
console.log(e);
try {
v = c[k].apply(c, []);
} catch (e) {
console.log(e);
}
}
logAccess(k, v);
if (shouldBind(v))
return wrap2(v)
else
return v;
}
let dog = (k) => {
res.__defineGetter__(k, () => {
const v = c[k]
logAccess(k, v);
if (shouldBind(v))
return wrap2(v)
else
return v;
})
}
let ks = new Set()
const travs = (c, s) => ((c != Object.getPrototypeOf({})) ? ((Object.getOwnPropertyNames(c).forEach(k => s.add(k))) + travs(Object.getPrototypeOf(c), s)) : null)
travs(c, ks)
ks = Array.from(ks)
ks.forEach(k => (isPropFunc(k) && !isProto(k)) && (res[k] = cat(k)))
ks.forEach(k => {
if (isProto(k)) {
// console.log(`binding proto ${k}`)
res[k] = c[k]
}
})
ks.forEach(k => (!isPropFunc(k)) && (dog(k)))
return res
}
run = () => {
html2canvas(wrap2(document.querySelector(".screenContent")), {allowTaint: true, foreignObjectRendering:true}).then(canvas => {
document.body.appendChild(canvas)
})
}
/*
let wrap = (c) => new Proxy(c, {
get: function(target, name) {
if (!(name in target)) {
console.log("Getting non-existant property '" + name + "'");
return undefined;
}
return target[name];
},
set: function(target, name, value) {
if (!(name in target)) {
console.log("Setting non-existant property '" + name + "', initial value: " + value);
}
target[name] = value;
}
});
res = new Set()
travv = (c) => ((c !== Object) ? (console.log(Object.keys(c)) + travv(Object.getPrototypeOf(c))) : null)
travs = (c, s) => ((c !== Object) ? ((Object.keys(c).forEach(k => s.add(k))) + travs(Object.getPrototypeOf(c), s)) : null)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment