Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * JDownloader2 EventScript - Auto Close showConfirmDialog
- *
- * @summary
- * Auto-close a dialog showed by showConfirmDialog() after "timeout" seconds.
- *
- * @event
- * Trigger: "Remote API Event fired"
- *
- * @usage
- *
- * 1. Install this script to your JD2.
- *
- * 2. Call showConfirmDialog() with special text
- *
- * ex) showConfirmDialog("Do you want to go?\n\ntimeout=10", "yes", "no");
- *
- * timeout:5
- * timeout=5
- * timeout=10, selected=ok
- * {timeout:30,selected:ok}
- * {"timeout":"30","selected":"ok"}
- *
- *
- * "timeout" value is second. (required)
- * "selected" value is "ok"(1st-button:return 1) or "cancel"(2nd-button:return 0).
- * (optional) no specify - default is "cancel"
- *
- * This is a tiny hack because showConfirmDialog does not have a timeout function.
- *
- * In the future, if showConfirmDialog() has TIMEOUT, this script will no longer be necessary.
- *
- */
- disablePermissionChecks();
- (function()
- {
- var _e = null;
- try{_e = event}catch(e){
- throw Error("ERROR: Trigger is not \"Remote API Event fired\".")
- }
- // "Test run" mode
- if (_e.publisher == "test" && _e.id == "test")
- {
- const t_now = getCurrentTimeStamp();
- const message = "Test Run mode\n\n\n\ntimeout=5";
- // const message = "Test Run mode\n\n\n\ntimeout=5,selected=ok";
- // const message = "Test Run mode\n\n\n\n{timeout:10}";
- const ret = showConfirmDialog(message,"OK","Cancel");
- alert("showConfirmDialog() selects \""+ret+"\", "+(getCurrentTimeStamp() - t_now)+" msec.");
- return;
- }
- // only "dialogs" "NEW" event.
- if (_e.publisher != "dialogs" || _e.id != "NEW")
- return;
- const MIN_TIMEOUT = 3;
- const MAX_TIMEOUT = 10 * 60; // sec , defalt
- // Disable Exception Dialog for callAPI's confusion
- setDisableOnException(false);
- setNotifyOnException(false);
- const dialog_info = callAPI("dialogs", "get", _e.data, false, true);
- // only "ConfirmDialogInterface"
- if (dialog_info.type != "org.appwork.uio.ConfirmDialogInterface")
- return;
- const r1 = dialog_info.properties.message.match(
- /"?\btimeout"?\s*[=:]\s*"?(\d+)/i //"
- );
- const r2 = dialog_info.properties.message.match(
- /"?\bselected"?\s*[=:]\s*"?(ok|cancel)"?/i
- );
- var timeout = 0;
- var selected = "cancel";
- if (r1) timeout = r1[1];
- if (r2) selected = r2[1];
- if (!timeout) return;
- timeout = Math.max(MIN_TIMEOUT, Math.min(MAX_TIMEOUT, timeout)).toFixed(0) * 1000;
- selected = selected.toLowerCase();
- sleep(timeout);
- if (callAPI("dialogs", "get", _e.data, false, false)) // Is the dialog still alive?
- {
- callAPI("dialogs", "answer", _e.data, {"closereason":selected});
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement