r/learnjavascript • • 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 this
butt.remove();*/
});
document.body.appendChild(ov);

/*And this
document.body.appendChild(butt);or ov.appendChild(butt) ?*/

})();
"""

def _show_edit_pause_overlay():
if not mw.reviewer or not mw.reviewer.web:
return
QTimer.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

4 Upvotes

7 comments sorted by

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 being null. Since you want those fields inside your overlay, you should create them as child elements and append them to ov instead of document.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/Foreign_Employ_396 21d ago

This worked thank you :)

I realized that my idea won't really work because it's attached to the main anki window and if I click on it, it will put itself and the whole anki window over the card editor tab (obviously but I really didn't think about it).

So now I am trying to see if I could make it a tab independant from the main one so that it can take one half of the screen like the editor.

My guess is that maybe if I can make this windows similar to the main one, the Yomilens addon might treat it the same and work there as well (?).

Another issue I discovered is that the content of the korean {{Back}} and eng {{Front}} fields that will be shown would be from the next card that I have not seen yet and not the one I just saw and am currently editing.

It makes sense since everything happens after I have rated the card, so technically the current card is the next one.

I tried something like that (but this might not be the right sub)

FIELD_FRONT = "Front"
FIELD_BACK = "Back"

def _get_previous_fields(self, card) -> str:
note = card.note()
r = self.mw.reviewer
previous = r.lastCard()
Front_value = note[FIELD_FRONT].strip() if FIELD_FRONT in note else ""
Back_value = note[FIELD_BACK].strip() if FIELD_BACK in note else ""

previous_korean = previous.Front_value if previous else "null"
previous_english = previous.Back_value if previous else "null"
return f"{previous_korean}/{previous_english}"

But the main issue is that I don't know how to create a pop up/windows

2

u/Puzzleheaded-Comb646 20d ago

You're very welcome! Since Anki add-ons use Python with Qt, you'll want to look into creating a QDialog or a custom QWidget to make a standalone pop-up window. You might also get great, specific help over at r/Anki for the Python API side—keep experimenting, you are making wonderful progress!

1

u/Hearthworks 21d ago

The reason the whole overlay stopped showing: butt.addEventListener(...) is live code, but the lines that declare and create butt are inside the /* ... */ comments. So the script throws ReferenceError: butt is not defined at that line, and never reaches document.body.appendChild(ov). Create the button before you attach the listener (and append it to ov, 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 in style.cssText anyway), and top:10 needs a unit like 10px.

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 korean and SizeBump live in the same web page as this overlay, or in the editor window? If they're in the editor, document.getElementById in the reviewer's page won't find them, and you'd need to pass their text in from Python instead.

1

u/Foreign_Employ_396 20d ago

Well technically (I noticed this afterwards) the korean and SizeBump texts I want to see while editing are not on the web page because anki has already moved on to the next card at that point.

I was thinking about trying to get the previous card's content, but if I can get them from the editor this might be a better way.

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) before document.body.appendChild(ov). and e.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.