Last active
April 14, 2023 23:41
-
-
Save Onurtag/ebb554cbc686f2b8429217420b1fe587 to your computer and use it in GitHub Desktop.
Auto hides yomichan popups when your mouse leaves the window.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Auto-Hide Yomichan Popups | |
// @description Auto-hides yomichan popups after your mouse leaves the window | |
// @namespace auto.hide.yomichan.popups | |
// @version 1.0.0 | |
// @author You | |
// @match https://anacreondjt.gitlab.io/texthooker.html | |
// @grant none | |
// @run-at document-idle | |
// ==/UserScript== | |
//❗ You need to disable the following yomichan setting for this to work. | |
//Security > "Use a secure container around popups" > turn it off. | |
//You can add more matched websites above or use your userscript extension to do the same. | |
//Auto-hide code from: https://github.com/Onurtag/ClipboardInserter_html | |
//Miliseconds delay to hide the popup after leaving the window | |
const hideDelay = 1000; | |
checkMouseInOut(); | |
//Mouse in/out check for yomichan (with shadow dom disabled in settings) | |
function checkMouseInOut() { | |
var theTimer; | |
var aFrameWasHidden = false; | |
//reset or clear the timer on mouse in/out | |
window.onmouseout = resetTimer; | |
window.onmouseover = clearTimer; | |
function doAction() { | |
try { | |
//hide yomichan if it exists. | |
let yomichanFrames = document.querySelectorAll("iframe.yomichan-popup"); | |
yomichanFrames.forEach(currFrame => { | |
if (currFrame.style.getPropertyValue("visibility") == "visible") { | |
currFrame.style.setProperty("visibility", "hidden", "important"); | |
currFrame.classList.add("thisFrameWasHidden"); | |
aFrameWasHidden = true; | |
} | |
}); | |
} catch (error) {} | |
} | |
function clearTimer(e) { | |
//Mouse entered the window or any element | |
clearTimeout(theTimer); | |
try { | |
if (aFrameWasHidden) { | |
let yomichanFrames = document.querySelectorAll("iframe.yomichan-popup"); | |
yomichanFrames.forEach(currFrame => { | |
if (currFrame.classList.contains("thisFrameWasHidden")) { | |
//transition is used to prevent flashing | |
currFrame.style.setProperty("transition", "visibility 0.5s linear 0.5s"); | |
currFrame.style.setProperty("visibility", "visible", "important"); | |
setTimeout(() => { | |
currFrame.style.removeProperty("transition"); | |
}, 1); | |
currFrame.classList.remove("thisFrameWasHidden"); | |
aFrameWasHidden = false; | |
} | |
}); | |
} | |
} catch (error) {} | |
} | |
function resetTimer(e) { | |
clearTimeout(theTimer); | |
let reltarget = e.relatedTarget || e.toElement; | |
if (!reltarget || (reltarget.nodeName == "HTML" && e.target == document.firstChild)) { | |
//Mouse left the window here, do action xxxx miliseconds after. | |
theTimer = setTimeout(doAction, hideDelay); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment