workaround for brave, wait till we get focus

This commit is contained in:
Sangeeth Sudheer 2023-01-05 01:01:12 +05:30
parent a51c1ebdb4
commit 8df5d36207
1 changed files with 36 additions and 3 deletions

View File

@ -1,7 +1,7 @@
// ==UserScript== // ==UserScript==
// @name LeetRay // @name LeetRay
// @namespace http://tampermonkey.net/ // @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 // @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 // @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 // @downloadURL https://git.sangeeth.dev/x/userscripts/raw/branch/main/leetray/LeetRay.user.js
@ -16,6 +16,7 @@
// @grant GM_log // @grant GM_log
// @grant GM_getTab // @grant GM_getTab
// @grant GM_setValue // @grant GM_setValue
// @grant GM_getValue
// @grant GM_addValueChangeListener // @grant GM_addValueChangeListener
// @grant GM_removeValueChangeListener // @grant GM_removeValueChangeListener
// @grant unsafeWindow // @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) { function debug(stuff) {
GM_log(`LeetRay DEBUG ${stuff}`); GM_log(`LeetRay DEBUG ${stuff}`);
} }
@ -61,6 +74,16 @@
GM_log(`LeetRay ERROR ${stuff}`); GM_log(`LeetRay ERROR ${stuff}`);
} }
async function isClipboardAllowedForRobots() {
try {
await navigator.clipboard.writeText("");
return true;
} catch (e) {
error(e);
return false;
}
}
function leetcodeMain() { function leetcodeMain() {
const menu_command_id = GM_registerMenuCommand( const menu_command_id = GM_registerMenuCommand(
"Copy screenshot", "Copy screenshot",
@ -117,7 +140,9 @@
await waitForRaysoExit(); await waitForRaysoExit();
window.focus(); if (GM_getValue("leetray.clip")) {
window.focus();
}
handler.onclose = () => { handler.onclose = () => {
alert("Copied screenshot"); alert("Copied screenshot");
@ -140,11 +165,19 @@
let id = GM_addValueChangeListener("leetray.screenshot", async () => { let id = GM_addValueChangeListener("leetray.screenshot", async () => {
window.focus(); window.focus();
await waitTillFocused();
const cbAllowed = await isClipboardAllowedForRobots();
await raysoCopyScreenshot(); await raysoCopyScreenshot();
GM_removeValueChangeListener(id); GM_removeValueChangeListener(id);
GM_setValue("leetray.rayso.exit", Date.now()); GM_setValue("leetray.rayso.exit", Date.now());
uwindow.close(); GM_setValue("leetray.clip", cbAllowed);
if (cbAllowed) {
uwindow.close();
}
}); });
} }