From 8df5d362070330c79a4b84b3403d279aa9df7fec Mon Sep 17 00:00:00 2001 From: Sangeeth Sudheer Date: Thu, 5 Jan 2023 01:01:12 +0530 Subject: [PATCH] workaround for brave, wait till we get focus --- leetray/LeetRay.user.js | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/leetray/LeetRay.user.js b/leetray/LeetRay.user.js index 4ac0e36..d100154 100644 --- a/leetray/LeetRay.user.js +++ b/leetray/LeetRay.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name LeetRay // @namespace http://tampermonkey.net/ -// @version 2023-01-04T01:21:05+05:30 +// @version 2023-01-05T01:00:39+05:30 // @description Take beautiful screenshots of your code in Leetcode instantly with Ray.so // @updateURL https://git.sangeeth.dev/x/userscripts/raw/branch/main/leetray/LeetRay.user.js // @downloadURL https://git.sangeeth.dev/x/userscripts/raw/branch/main/leetray/LeetRay.user.js @@ -16,6 +16,7 @@ // @grant GM_log // @grant GM_getTab // @grant GM_setValue +// @grant GM_getValue // @grant GM_addValueChangeListener // @grant GM_removeValueChangeListener // @grant unsafeWindow @@ -53,6 +54,18 @@ }); } + async function waitTillFocused() { + return new Promise((resolve) => { + setTimeout(function check() { + if (document.visibilityState === "visible") { + resolve(); + } else { + setTimeout(check, 500); + } + }, 500); + }); + } + function debug(stuff) { GM_log(`LeetRay DEBUG ${stuff}`); } @@ -61,6 +74,16 @@ GM_log(`LeetRay ERROR ${stuff}`); } + async function isClipboardAllowedForRobots() { + try { + await navigator.clipboard.writeText(""); + return true; + } catch (e) { + error(e); + return false; + } + } + function leetcodeMain() { const menu_command_id = GM_registerMenuCommand( "Copy screenshot", @@ -117,7 +140,9 @@ await waitForRaysoExit(); - window.focus(); + if (GM_getValue("leetray.clip")) { + window.focus(); + } handler.onclose = () => { alert("Copied screenshot"); @@ -140,11 +165,19 @@ let id = GM_addValueChangeListener("leetray.screenshot", async () => { window.focus(); + await waitTillFocused(); + + const cbAllowed = await isClipboardAllowedForRobots(); + await raysoCopyScreenshot(); GM_removeValueChangeListener(id); GM_setValue("leetray.rayso.exit", Date.now()); - uwindow.close(); + GM_setValue("leetray.clip", cbAllowed); + + if (cbAllowed) { + uwindow.close(); + } }); }