Guest User

Untitled

a guest
Jan 23rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. document.createElement = function (
  2. createElement, // the native one
  3. createResponse // the function "in charge"
  4. ) {
  5. return function (nodeName) {
  6. var result, src;
  7. // if we are creating a script
  8. if (/^script$/i.test(nodeName)) {
  9. // result will be a place holder
  10. result = createElement.call(
  11. document,
  12. "meta"
  13. );
  14. // we need to monitor the src property
  15. Object.defineProperty(result, "src", {
  16. get: function () {
  17. return src;
  18. },
  19. // when set ...
  20. set: function ($src) {
  21. // we can check periodically ...
  22. function T() {
  23. // if the placeholder is in DOM
  24. if (result.parentNode) {
  25. // in this case we can put a real script
  26. result = result.parentNode.insertBefore(
  27. createElement.call(
  28. document,
  29. "script"
  30. ),
  31. result
  32. );
  33. // and set the encoded src
  34. result.src = "data:text/javascript;base64," +
  35. btoa(createResponse.call(result, src))
  36. ;
  37. } else {
  38. // no DOM, no loading ... try later
  39. setTimeout(T, 100);
  40. }
  41. }
  42. // store the src
  43. src = $src;
  44. // and start checking
  45. T();
  46. }
  47. });
  48. } else {
  49. // just return the element
  50. result = createElement.call(
  51. document,
  52. nodeName
  53. );
  54. }
  55. return result;
  56. };
  57. }(
  58. document.createElement,
  59.  
  60. // must return a string
  61. function (src) {
  62.  
  63. // this points to the current script
  64. // src is the address
  65. // if we know the callback ...
  66.  
  67. if (/callback=([^&]+)/.test(src)) {
  68. return RegExp.$1 + '(' +
  69. JSON.stringify({dummy:"data"})
  70. + ')';
  71. }
  72. }
  73. );
  74.  
  75.  
  76. // example
  77. function test(data) {
  78. alert(data.dummy);
  79. }
  80.  
  81. document.documentElement.insertBefore(
  82. document.createElement("script"),
  83. document.documentElement.lastChild
  84. ).src = "page.php?callback=test";
Add Comment
Please, Sign In to add comment