Skip to content

Instantly share code, notes, and snippets.

@dialtone
Last active December 21, 2015 16:18
Show Gist options
  • Save dialtone/6332284 to your computer and use it in GitHub Desktop.
Save dialtone/6332284 to your computer and use it in GitHub Desktop.
var _payloadArgs = null;
function installMonitor(){
var rootElement = document.body || document.head || document.documentElement;
// Create communications dom elem
var div = $('<div/>').attr({"id": "unique"}).css({display: "none"});
div.appendTo(mainElement);
// this is our click handler that the injected js will call.
div[0].onclick = function(){
// Save the vars and the interval below will pick them up.
_payloadVars = [this.args];
};
// actually request the payload when the _payload vars are set.
var interval = setInterval(function(){
if(_payloadArgs){
getPayload(_payloadArgs, function(payload){
MYOBJECT.doSomething(payload, _payloadArgs);
});
clearInterval(interval);
}
}, 100);
var script = $("<script />");
script.text("(" + __monitorForPayload + ")();");
script.appendTo(rootElement);
}
// Function injected into the actual page
function __monitorForPayload(){
var oldCallback = null; // old version of the function
// The pixel payload will call this function
var callback = function (args){
// create the new div, set it's attributes and values to pick up later
var newSpan = $("<span />").attr({"type": "hidden",
"id": "unique-2",
"args": "some-variable"});
newSpan.insertBefore(document.body.firstChild);
// Make the callback to the contentscript's fn.
var elem = document.getElementById("unique");
if (elem && typeof elem.onclick == "function") {
elem.args = "some-variable"; //ghetto
elem.onclick.apply(elem);
}
return oldSetPC.call(this, args);
};
// We dont know when the object will be available, so we poll.
var interval = setInterval(function(){
if(window.__some_class){
oldCallback = __some_class.callback;
__some_class.callback = callback;
clearInterval(interval);
}
}, 20);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment