r/learnjavascript • u/Foreign_Employ_396 • 22d ago
How to add elements (child) over an overlay ?
This function was initially creating an overlay appearing while I was modifying a card on anki.
For context, I started using the yomilens addon on anki which allows you to select some text and have a pop up show you the translation. The thing is, it only works on the "main" reviewer windows of the anki app. And what I want i to be able to select an english word and add many (if not all) of the ways to say it in korean in my card.
My first thought was to try to "force" the current card to keep showing me it's back with all the texts I need, but I couldn't.
Now I am trying to add the content of the two fields (id"korean" and id"SizeBump"=english) on the overlay, but being selectable to see if yomilens will react to it.
def pause_audio_for_edit():
/* Irrelevant I think
global _audio_paused_for_edit, _pending_audio_tags
print("[pause_audio] pause_audio_for_edit() appelée")
_audio_paused_for_edit = True
_pending_audio_tags = None
_force_stop_current_audio()*/
_show_edit_pause_overlay()
_EDIT_PAUSE_OVERLAY_JS = """
(function(){
var ov = document.getElementById('_editPauseOverlay');
/* Added
var butt = document.getElementById('button');
var board = document.getElementById('_editboard');
var kor = document.getElementById('korean');
var eng = document.getElementById('SizeBump'); */
if (ov) return;
ov = document.createElement('div');
ov.id = '_editPauseOverlay';
ov.style.cssText = 'position:fixed;top:0;left:0;width:100%;height:100%;'
+ 'background:rgba(20,20,20,0.96);color:#f7b799;display:flex;'
+ 'align-items:center;justify-content:center;text-align:center;'
+ 'font-family:sans-serif;font-size:16px;z-index:99999;cursor:pointer;'
+ 'padding:20px;box-sizing:border-box;';
ov.innerHTML = '\\u23F8\\uFE0F En pause \\u2014 modification en cours<br>'
+ '<span style="font-size:12px;opacity:0.7;">(cliquez n\\'importe o\\u00f9 pour reprendre)</span>';
/*added
+ '<span id='korean'</span> <span id='SizeBump'</span>';
butt = document.createElement('button');
butt.id
= 'button';
butt.style.cssText = 'position:fixed;top:10;left:0;width:10%;height:8%;'
+ 'background:rgba(255,255,255,0.6);color:#a77f65;display:flex;'
+ 'font-family:sans-serif;font-size:16px;font-weight: bold;z-index:99999;cursor:pointer;'
+ 'padding: 4px 12px;box-sizing:border-box;border-radius: 14px;margin: 10px auto 0 auto;border: 1px solid #a77f65;'
+ 'cursor: pointer;';
+ 'hover {background: rgba(255,255,255,0.9);}
butt.innerHTML = 'click when finished'*/// Used to be ov.addEventListener...butt.addEventListener('click', function(){if (typeof pycmd === 'function') pycmd('editpause:dismiss');ov.remove();
/*added thisbutt.remove();*/});document.body.appendChild(ov);
/*And thisdocument.body.appendChild(butt);or ov.appendChild(butt) ?*/
})();"""
def _show_edit_pause_overlay():if not mw.reviewer or not mw.reviewer.web:returnQTimer.singleShot(20, lambda: mw.reviewer.web.eval(_EDIT_PAUSE_OVERLAY_JS))
And as you can probably guess the overlay is not working at all now. Do you guys know what I have to fix to make it work ?
Thank you
1
u/Puzzleheaded-Comb646 22d ago
You are actually really close! The main issue is that you are trying to grab elements like
document.getElementById('korean')at the very top of your script before you have actually created them, so those variables end up beingnull. Since you want those fields inside your overlay, you should create them as child elements and append them toovinstead ofdocument.body.Try structuring it like this inside your IIFE:
javascript var ov = document.createElement('div'); ov.id = '_editPauseOverlay'; ov.style.cssText = 'position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(20,20,20,0.96);color:#f7b799;display:flex;flex-direction:column;align-items:center;justify-content:center;z-index:99999;';
var kor = document.createElement('span'); kor.id = 'korean'; kor.innerText = 'Your Korean text here';
var butt = document.createElement('button'); butt.id = 'button'; butt.innerText = 'click when finished'; butt.addEventListener('click', function(e){ e.stopPropagation(); // prevents clicking the overlay itself if (typeof pycmd === 'function') pycmd('editpause:dismiss'); ov.remove(); });
ov.appendChild(kor); ov.appendChild(butt); document.body.appendChild(ov);
Also,
1
u/Hearthworks 21d ago
The reason the whole overlay stopped showing:
butt.addEventListener(...)is live code, but the lines that declare and createbuttare inside the/* ... */comments. So the script throwsReferenceError: butt is not definedat that line, and never reachesdocument.body.appendChild(ov). Create the button before you attach the listener (and append it toov, as the other comment shows), and the overlay will come back. The commented-out code also needs tidying before you uncomment it: the<span id='korean'...line has clashing quotes and no>, the+ 'hover {...}line is an unclosed string (hover styles can't go instyle.cssTextanyway), andtop:10needs a unit like10px.Since you want the text to be selectable, a separate button to close the overlay is the right call, rather than closing it on any click.
One question that decides the next step: while you're editing, do
koreanandSizeBumplive in the same web page as this overlay, or in the editor window? If they're in the editor,document.getElementByIdin the reviewer's page won't find them, and you'd need to pass their text in from Python instead.1
u/Discordbothost 18d ago
yeah the overlay died because the button/append bits are still stuck inside comments — js never runs them.
uncomment, create the button with createElement (not broken innerHTML), then
ov.appendChild(butt)beforedocument.body.appendChild(ov). ande.stopPropagation()on the button click so dismissing the overlay doesn't fire when you hit the button. once those live as real statements again it should show.