Skip to content

Instantly share code, notes, and snippets.

@derjanb
Last active February 21, 2021 04:47
Show Gist options
  • Save derjanb/5385914 to your computer and use it in GitHub Desktop.
Save derjanb/5385914 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Test Window 27
// @namespace http://use.i.E.your.homepage/
// @version 0.2
// @description enter something useful
// @match http://tampermonkey.net/test/jquery.html
// @run-at document-body
// @copyright 2012+, You
// ==/UserScript==
var use = { safeContext: true };
var Eventing = { contextId: 498948, eventId: 'foo' };
var TM_do = function(src) {
if (use.safeContext) {
var msg = null;
if (typeof src === 'object') {
msg = JSON.stringify(src);
} else {
msg = JSON.stringify( { method: 'eval', arg: src } );
}
var customEvent = document.createEvent("MutationEvent");
customEvent.initMutationEvent(Eventing.eventId + '#' + Eventing.contextId,
false,
false,
null,
null,
null,
msg,
customEvent.ADDITION);
document.dispatchEvent(customEvent);
return customEvent.returnValue;
} else {
console.log("ERROR: assert(use.safeContext)");
}
}
var installPageEventHandler = function(w) {
var eventHandler = function(evt) {
var info = null, passenger;
try {
info = JSON.parse(evt.attrName);
passenger = window['passenger_' + evt.type.replace(/[^#]*#/, '')];
if (info.method == 'get') {
var p = info.path.split('.');
var obj = window;
for (var i=0; i<p.length; i++) {
obj = obj[p[i]];
}
var m = obj[info.name];
var r = {};
// if (typeof m === 'function') {
// r.function = true;
// } else {
r.value = m;
// }
passenger['_' + info.id] = r;
// } else if (info.method == 'execute') {
} else if (info.method == 'set') {
var p = info.path.split('.');
var obj = window;
for (var i=0; i<p.length; i++) {
obj = obj[p[i]];
}
var arg = passenger[info.id];
obj[info.name] = arg.value;
passenger['_' + info.id] = {};
} else if (info.method == 'delete') {
var p = info.path.split('.');
var obj = window;
for (var i=0; i<p.length; i++) {
obj = obj[p[i]];
}
delete obj[info.name];
passenger['_' + info.id] = {};
} else if (info.method == 'eval') {
___eval___(info.arg);
}
if (info.id) {
delete passenger[info.id];
}
} catch (e) {}
evt = null;
};
var is = "document.addEventListener('" + Eventing.eventId +'#'+ Eventing.contextId+"', " + eventHandler.toString().replace('___eval___', 'eval') + ", false);\n";
var s = w.document.createElement('script');
s.setAttribute('name', "TM_internal");
s.innerHTML = is;
var d = w.document;
(d.documentElement || d).appendChild(s);
s.parentNode.removeChild(s);
};
var getPageObject = function(o) {
var yippieYeah = document.createElement("div");
var ret = null;
try {
yippieYeah.setAttribute("onclick", "return " + o + ";");
ret = yippieYeah.onclick();
} catch (e) {}
yippieYeah.setAttribute("onclick", null);
yippieYeah.onclick = null;
yippieYeah = null;
return ret;
};
var createPassenger = function() {
var name = 'passenger_' + Eventing.contextId;
var rg = '';
rg += "var prop = {};\n";
rg += "var passenger = {};\n";
rg += "prop['" + name + "'] = {\n";
rg += " value: passenger,\n";
rg += " enumerable: false,\n";
rg += " writable: false,\n";
rg += " configurable: false\n";
rg += "};\n";
rg += "Object.defineProperties(window, prop);\n";
// add passenger object to pageWindow
TM_do(rg);
var values = getPageObject(name);
var passenger = (function(name) {
return {
get: function(id) {
var r = values[id];
delete values[id];
return r;
},
set: function(args) {
var id = 0;
while (id == 0 || values[id] !== undefined) {
id = (new Date()).getTime() + Math.floor ( Math.random ( ) * 12061984 + 1 );
}
values[id] = args || {};
return id;
},
getName : function() {
return name;
}
};
})(name);
return passenger;
};
var createProxiedObject = function(path, passenger) {
var proxy = Proxy.create({
get: function(proxy, name) {
var id = passenger.set({});
TM_do({ method: 'get', name: name, id: id, path: path });
var ret = passenger.get('_' + id);
return ret.value;
},
set: function(proxy, name, value) {
var id = passenger.set({ value: value });
TM_do({ method: 'set', name: name, id: id, path: path });
var ret = passenger.get('_' + id);
return ret.value;
},
'delete': function(proxy, name) {
var id = passenger.set({});
TM_do({ method: 'delete', name: name, id: id, path: path });
var ret = passenger.get('_' + id);
return ret.value;
}
});
return proxy;
};
var prepareUnsafeWindowForChrome27 = function(passenger) {
var ret;
ret = createProxiedObject('window', passenger);
return ret;
};
installPageEventHandler(window, 'foo');
var rwin = prepareUnsafeWindowForChrome27(createPassenger());
var r = rwin.run;
console.log(r);
debugger;
r();
@vendethiel
Copy link

broken already

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment