Advertisement
usamimi2323

JDownloader2 EventScript - Auto Close showConfirmDialog

Feb 17th, 2025 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.67 KB | Source Code | 0 0
  1. /**
  2.  * JDownloader2 EventScript - Auto Close showConfirmDialog
  3.  *
  4.  * @summary
  5.  *     Auto-close a dialog showed by showConfirmDialog() after "timeout" seconds.
  6.  *
  7.  * @event
  8.  *     Trigger: "Remote API Event fired"
  9.  *
  10.  * @usage
  11.  *
  12.  * 1. Install this script to your JD2.
  13.  *
  14.  * 2. Call showConfirmDialog() with special text
  15.  *
  16.  *     ex) showConfirmDialog("Do you want to go?\n\ntimeout=10", "yes", "no");
  17.  *
  18.  *     timeout:5
  19.  *     timeout=5
  20.  *     timeout=10, selected=ok
  21.  *     {timeout:30,selected:ok}
  22.  *     {"timeout":"30","selected":"ok"}
  23.  *
  24.  *
  25.  * "timeout" value is second. (required)
  26.  * "selected" value is "ok"(1st-button:return 1) or "cancel"(2nd-button:return 0).
  27.  *     (optional) no specify - default is "cancel"
  28.  *
  29.  * This is a tiny hack because showConfirmDialog does not have a timeout function.
  30.  *
  31.  * In the future, if showConfirmDialog() has TIMEOUT, this script will no longer be necessary.
  32.  *
  33.  */
  34. disablePermissionChecks();
  35.  
  36.  
  37. (function()
  38. {
  39.     var _e = null;
  40.     try{_e = event}catch(e){
  41.         throw Error("ERROR: Trigger is not \"Remote API Event fired\".")
  42.     }
  43.    
  44.     // "Test run" mode
  45.     if (_e.publisher == "test" && _e.id == "test")
  46.     {
  47.         const t_now = getCurrentTimeStamp();
  48.        
  49.         const message = "Test Run mode\n\n\n\ntimeout=5";
  50. //      const message = "Test Run mode\n\n\n\ntimeout=5,selected=ok";
  51. //      const message = "Test Run mode\n\n\n\n{timeout:10}";
  52.        
  53.         const ret = showConfirmDialog(message,"OK","Cancel");
  54.        
  55.         alert("showConfirmDialog() selects \""+ret+"\", "+(getCurrentTimeStamp() - t_now)+" msec.");
  56.        
  57.         return;
  58.     }
  59.    
  60.     // only "dialogs" "NEW" event.
  61.     if (_e.publisher != "dialogs" || _e.id != "NEW")
  62.         return;
  63.    
  64.     const MIN_TIMEOUT = 3;
  65.     const MAX_TIMEOUT = 10 * 60;       // sec , defalt
  66.    
  67.     // Disable Exception Dialog for callAPI's confusion
  68.     setDisableOnException(false);
  69.     setNotifyOnException(false);
  70.  
  71.     const dialog_info = callAPI("dialogs", "get", _e.data, false, true);
  72.  
  73.     // only "ConfirmDialogInterface"
  74.     if (dialog_info.type != "org.appwork.uio.ConfirmDialogInterface")
  75.         return;
  76.    
  77.     const r1 = dialog_info.properties.message.match(
  78.         /"?\btimeout"?\s*[=:]\s*"?(\d+)/i   //"
  79.     );
  80.     const r2 = dialog_info.properties.message.match(
  81.         /"?\bselected"?\s*[=:]\s*"?(ok|cancel)"?/i
  82.     );
  83.    
  84.     var timeout = 0;
  85.     var selected = "cancel";
  86.     if (r1) timeout = r1[1];
  87.     if (r2) selected = r2[1];
  88.     if (!timeout) return;
  89.    
  90.     timeout  = Math.max(MIN_TIMEOUT, Math.min(MAX_TIMEOUT, timeout)).toFixed(0) * 1000;
  91.     selected = selected.toLowerCase();
  92.    
  93.     sleep(timeout);
  94.    
  95.     if (callAPI("dialogs", "get", _e.data, false, false))   // Is the dialog still alive?
  96.     {
  97.         callAPI("dialogs", "answer", _e.data, {"closereason":selected});
  98.     }
  99. })();
  100.  
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement