Last active
December 17, 2015 12:59
-
-
Save AutoSponge/5614189 to your computer and use it in GitHub Desktop.
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>fake ajax</title> | |
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> | |
<script src="fake.js"></script> | |
<script src="config.js"></script> | |
<script> | |
/*implement*/ | |
$(function () { | |
$("#myButton").click(function () { | |
$.get("http://test.com/data", function (response, raw) { | |
$("#data").text($("#data").text() + raw); | |
}); | |
}); | |
}); | |
$(function () { | |
$("#download").click(function () { | |
var href = "data:text/plain;charset=utf-8,"; | |
href += encodeURI($("#data").val()); | |
this.href = href; | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<button id="myButton">get data</button> | |
<a id="download" download="test.txt">download</a> | |
<div></div> | |
<textarea id="data" style="height: 400px;"></textarea> | |
</body> | |
</html> |
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
$$.registerUrl("http://test.com/data", "data.html"); |
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
<script src="include.js"> | |
test | |
</script> |
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
(function (global) { | |
var body = null; | |
var deferreds = {}; | |
var config = {}; | |
function registerUrl(url, filePath) { | |
config[url] = filePath; | |
} | |
function registerUrls(tuples) { | |
if (typeof tuples === "string") { | |
return registerUrl.apply(null, arguments); | |
} | |
var i, len; | |
for (i = 0, len = tuples.length; i < len; i += 1) { | |
registerUrl(tuples[i][0], tuples[i][1]); | |
} | |
} | |
function generateHeader(id) { | |
return id + "\n"; | |
} | |
function handleMessage(msg) { | |
var file = msg.data.match(/.+/)[0]; | |
var raw = msg.data.replace(/.+\n\s*/, ""); | |
var data = raw; | |
try { | |
data = JSON.parse(raw); | |
} catch(e) {} | |
deferreds[file].resolveWith(null, [data, raw]); | |
$("iframe[src='" + file + "']").remove(); | |
} | |
function handleLoad(elm) { | |
elm.contentWindow.postMessage(generateHeader(elm.getAttribute("src")), "*"); | |
} | |
function createFrame(file) { | |
body = body || $("body"); | |
if (!body.length) { | |
return setTimeout(function () { | |
createFrame(file); | |
}, 100); | |
} | |
return $("<iframe src='" + file + | |
"' onload='$$.handleLoad(this);' " + | |
"style='width:0;height:0;border:0;'></iframe>").appendTo(body); | |
} | |
function fakeAjax(url, callback) { | |
var obj, dfd, file, success; | |
if (typeof url === "string") { | |
success = callback; | |
file = config[url]; | |
} else { | |
obj = url; | |
success = obj.success; | |
file = config[obj.url]; | |
} | |
deferreds[file] = dfd = $.Deferred(); | |
dfd.done(success); | |
createFrame(file); | |
return dfd.promise(); | |
} | |
try { | |
global.addEventListener("message", handleMessage, false); | |
} catch (e) { | |
global.attachEvent("onmessage", handleMessage, false); | |
} | |
global.$$ = {}; | |
global.$$.handleLoad = handleLoad; | |
global.$$.registerUrl = registerUrls; | |
global.$.ajax = fakeAjax; | |
}(this)); |
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
(function () { | |
var scripts = document.getElementsByTagName("script"); | |
var payload = scripts[ scripts.length - 1 ].innerHTML; | |
function handleMessage(e) { | |
e.source.postMessage(e.data + payload, "*"); | |
} | |
try { | |
this.addEventListener("message", handleMessage, false); | |
} catch (e) { | |
this.attachEvent("onmessage", handleMessage); | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment