Advertisement
CR7CR7

prompt

Jun 8th, 2023
1,323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Variables for prompt
  2. var originalPrompt = window.prompt;
  3. var nextPromptResult = false;
  4. var recordedPrompt = null;
  5.  
  6. // Variables for confirm
  7. var originalConfirmation = window.confirm;
  8. var nextConfirmationResult = false;
  9. var recordedConfirmation = null;
  10.  
  11. // Variables for alert
  12. var originalAlert = window.alert;
  13. var nextAlertResult = false;
  14. var recordedAlert = null;
  15.  
  16. // Function to get the frame location
  17. function getFrameLocation() {
  18.   let frameLocation = "";
  19.   let currentWindow = window;
  20.   let currentParentWindow;
  21.   while (currentWindow !== window.top) {
  22.     currentParentWindow = currentWindow.parent;
  23.     for (let idx = 0; idx < currentParentWindow.frames.length; idx++) {
  24.       if (currentParentWindow.frames[idx] === currentWindow) {
  25.         frameLocation = ":" + idx + frameLocation;
  26.         currentWindow = currentParentWindow;
  27.         break;
  28.       }
  29.     }
  30.   }
  31.   frameLocation = "root" + frameLocation;
  32.   return frameLocation;
  33. }
  34.  
  35. // Function to handle prompt in non-top windows
  36. function handlePromptInNonTopWindows(text, defaultText) {
  37.   if (document.body.hasAttribute("SideeXPlayingFlag")) {
  38.     return window.top.prompt(text, defaultText);
  39.   } else {
  40.     let result = originalPrompt(text, defaultText);
  41.     let frameLocation = getFrameLocation();
  42.     window.top.postMessage({
  43.       direction: "from-page-script",
  44.       recordedType: "prompt",
  45.       recordedMessage: text,
  46.       recordedResult: result,
  47.       frameLocation: frameLocation
  48.     }, "*");
  49.     return result;
  50.   }
  51. }
  52.  
  53. // Function to handle confirm in non-top windows
  54. function handleConfirmInNonTopWindows(text) {
  55.   if (document.body.hasAttribute("SideeXPlayingFlag")) {
  56.     return window.top.confirm(text);
  57.   } else {
  58.     let result = originalConfirmation(text);
  59.     let frameLocation = getFrameLocation();
  60.     window.top.postMessage({
  61.       direction: "from-page-script",
  62.       recordedType: "confirm",
  63.       recordedMessage: text,
  64.       recordedResult: result,
  65.       frameLocation: frameLocation
  66.     }, "*");
  67.     return result;
  68.   }
  69. }
  70.  
  71. // Function to handle alert in non-top windows
  72. function handleAlertInNonTopWindows(text) {
  73.   if (document.body.hasAttribute("SideeXPlayingFlag")) {
  74.     recordedAlert = text;
  75.     window.top.postMessage({
  76.       direction: "from-page-script",
  77.       response: "alert",
  78.       value: recordedAlert
  79.     }, "*");
  80.     return;
  81.   } else {
  82.     let result = originalAlert(text);
  83.     let frameLocation = getFrameLocation();
  84.     window.top.postMessage({
  85.       direction: "from-page-script",
  86.       recordedType: "alert",
  87.       recordedMessage: text,
  88.       recordedResult: result,
  89.       frameLocation: frameLocation
  90.     }, "*");
  91.     return result;
  92.   }
  93. }
  94.  
  95. // Function to handle prompt in top window
  96. function handlePromptInTopWindow(text, defaultText) {
  97.   if (document.body.hasAttribute("setPrompt")) {
  98.     recordedPrompt = text;
  99.     document.body.removeAttribute("setPrompt");
  100.     return nextPromptResult;
  101.   } else {
  102.     let result = originalPrompt(text, defaultText);
  103.     let frameLocation = getFrameLocation();
  104.     window.top.postMessage({
  105.       direction: "from-page-script",
  106.       recordedType: "prompt",
  107.       recordedMessage: text,
  108.       recordedResult: result,
  109.       frameLocation: frameLocation
  110.     }, "*");
  111.     return result;
  112.   }
  113. }
  114.  
  115. // Function to handle confirm in top window
  116. function handleConfirmInTopWindow(text) {
  117.   if (document.body.hasAttribute("setConfirm")) {
  118.     recordedConfirmation = text;
  119.     document.body.removeAttribute("setConfirm");
  120.     return nextConfirmationResult;
  121.   } else {
  122.     let result = originalConfirmation(text);
  123.     let frameLocation = getFrameLocation();
  124.     window.top.postMessage({
  125.       direction: "from-page-script",
  126.       recordedType: "confirm",
  127.       recordedMessage: text,
  128.       recordedResult: result,
  129.       frameLocation: frameLocation
  130.     }, "*");
  131.     return result;
  132.   }
  133. }
  134.  
  135. // Function to handle alert in top window
  136. function handleAlertInTopWindow(text) {
  137.   if (document.body.hasAttribute("SideeXPlayingFlag")) {
  138.     recordedAlert = text;
  139.     window.top.postMessage({
  140.       direction: "from-page-script",
  141.       response: "alert",
  142.       value: recordedAlert
  143.     }, "*");
  144.     return;
  145.   } else {
  146.     let result = originalAlert(text);
  147.     let frameLocation = getFrameLocation();
  148.     window.top.postMessage({
  149.       direction: "from-page-script",
  150.       recordedType: "alert",
  151.       recordedMessage: text,
  152.       recordedResult: result,
  153.       frameLocation: frameLocation
  154.     }, "*");
  155.     return result;
  156.   }
  157. }
  158.  
  159. // Override prompt function
  160. if (window !== window.top) {
  161.   window.prompt = function(text, defaultText) {
  162.     return handlePromptInNonTopWindows(text, defaultText);
  163.   };
  164.  
  165.   window.confirm = function(text) {
  166.     return handleConfirmInNonTopWindows(text);
  167.   };
  168.  
  169.   window.alert = function(text) {
  170.     return handleAlertInNonTopWindows(text);
  171.   };
  172. } else {
  173.   window.prompt = function(text, defaultText) {
  174.     return handlePromptInTopWindow(text, defaultText);
  175.   };
  176.  
  177.   window.confirm = function(text) {
  178.     return handleConfirmInTopWindow(text);
  179.   };
  180.  
  181.   window.alert = function(text) {
  182.     return handleAlertInTopWindow(text);
  183.   };
  184. }
  185.  
  186. // Play window methods
  187. if (window === window.top) {
  188.   window.addEventListener("message", function(event) {
  189.     if (event.source === window && event.data && event.data.direction === "from-content-script") {
  190.       let result = undefined;
  191.       switch (event.data.command) {
  192.         case "setNextPromptResult":
  193.           nextPromptResult = event.data.target;
  194.           document.body.setAttribute("setPrompt", true);
  195.           window.postMessage({
  196.             direction: "from-page-script",
  197.             response: "prompt"
  198.           }, "*");
  199.           break;
  200.         case "getPromptMessage":
  201.           result = recordedPrompt;
  202.           recordedPrompt = null;
  203.           window.postMessage({
  204.             direction: "from-page-script",
  205.             response: "prompt",
  206.             value: result
  207.           }, "*");
  208.           break;
  209.         case "setNextConfirmationResult":
  210.           nextConfirmationResult = event.data.target;
  211.           document.body.setAttribute("setConfirm", true);
  212.           window.postMessage({
  213.             direction: "from-page-script",
  214.             response: "confirm"
  215.           }, "*");
  216.           break;
  217.         case "getConfirmationMessage":
  218.           result = recordedConfirmation;
  219.           recordedConfirmation = null;
  220.           try {
  221.             console.error("no");
  222.             window.postMessage({
  223.               direction: "from-page-script",
  224.               response: "confirm",
  225.               value: result
  226.             }, "*");
  227.           } catch (e) {
  228.             console.error(e);
  229.           }
  230.           break;
  231.         case "setNextAlertResult":
  232.           nextAlertResult = event.data.target;
  233.           document.body.setAttribute("setAlert", true);
  234.           window.postMessage({
  235.             direction: "from-page-script",
  236.             response: "alert"
  237.           }, "*");
  238.           break;
  239.       }
  240.     }
  241.   });
  242. }
  243.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement