Created
August 24, 2011 19:03
-
-
Save WebReflection/1168884 to your computer and use it in GitHub Desktop.
hot to mock script requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.createElement = function ( | |
createElement, // the native one | |
createResponse // the function "in charge" | |
) { | |
return function (nodeName) { | |
var result, src; | |
// if we are creating a script | |
if (/^script$/i.test(nodeName)) { | |
// result will be a place holder | |
result = createElement.call( | |
document, | |
"meta" | |
); | |
// we need to monitor the src property | |
Object.defineProperty(result, "src", { | |
get: function () { | |
return src; | |
}, | |
// when set ... | |
set: function ($src) { | |
// we can check periodically ... | |
function T() { | |
// if the placeholder is in DOM | |
if (result.parentNode) { | |
// in this case we can put a real script | |
result = result.parentNode.insertBefore( | |
createElement.call( | |
document, | |
"script" | |
), | |
result | |
); | |
// and set the encoded src | |
result.src = "data:text/javascript;base64," + | |
btoa(createResponse.call(result, src)) | |
; | |
} else { | |
// no DOM, no loading ... try later | |
setTimeout(T, 100); | |
} | |
} | |
// store the src | |
src = $src; | |
// and start checking | |
T(); | |
} | |
}); | |
} else { | |
// just return the element | |
result = createElement.call( | |
document, | |
nodeName | |
); | |
} | |
return result; | |
}; | |
}( | |
document.createElement, | |
// must return a string | |
function (src) { | |
// this points to the current script | |
// src is the address | |
// if we know the callback ... | |
if (/callback=([^&]+)/.test(src)) { | |
return RegExp.$1 + '(' + | |
JSON.stringify({dummy:"data"}) | |
+ ')'; | |
} | |
} | |
); | |
// example | |
function test(data) { | |
alert(data.dummy); | |
} | |
document.documentElement.insertBefore( | |
document.createElement("script"), | |
document.documentElement.lastChild | |
).src = "page.php?callback=test"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment