krassowski / Anki-Night-Mode

Enabling studying at night with Anki - without eye strain
https://ankiweb.net/shared/info/1496166067
GNU General Public License v3.0
72 stars 13 forks source link

Support .night-Class #1

Closed colchizin closed 9 years ago

colchizin commented 9 years ago

Hey, first off, thanks for the great plugin!

One issue is Anki's missing support for the .night-Class, that one can use in AnkiMobile.

One solution would be to overwrite anki's method for generating the card's HTML (originally at anki/ankiqt/webview:110) in you plugin:

from anki.utils import isMac, isWin
from anki import js
import anki
from aqt import webview

def stdHtml(self, body, css="", bodyClass="", loadCB=None, js=None, head=""):
    # this is the only thing new:
    if nm_state_on:
        bodyClass = bodyClass + " night"

    if isMac:
        button = "font-weight: bold; height: 24px;"
    else:
        button = "font-weight: normal;"
    self.setHtml("""
<!doctype html>
<html><head><style>
button {
%s
}
%s</style>
<script>%s</script>
%s

</head>
<body class="%s">%s</body></html>""" % (
    button, css, js or anki.js.jquery+anki.js.browserSel,
    head, bodyClass, body), loadCB)

# this is where we actually overwrite the original method
webview.AnkiWebView.stdHtml = stdHtml

This does of course have the disadvantage, that any addon or anki-update, that changes the behavior of the stdHtml-Method, will ultimately be overwritten by this, too

Best Regards!

krassowski commented 9 years ago

Thanks for your investigation!

I will take a look on this issue after Wednesday. At a glance I can say that there are other possible ways to introduce a name of class into the HTML elements - for example with use of javascript.

Any method has pros and cons; as you mentioned, overwriting original functions is demanding in terms of code maintenance (keeping up to date with new Anki releases) but it will be better in terms of code readability. I'm not sure whether use of js will give good performance (ie. whether it will be free of delayed rendering issue).

After some tests and deeper analyse of code I will update add-on and post info there.

krassowski commented 9 years ago

I've updated the add-on. Now there is the "night_mode" class available (the same which is used by AnkiMobile). It's implemented with use of javascript and Anki hooks (in the original code names of classes are handled also with js).

It will be great if you could acknowledge that it's working as expected. To update add-on follow the "Manual Installation" instruction in readme file.

I hope it helped :)

colchizin commented 9 years ago

Hi,

a much more elegant solution, thank you very much. Works as expected for studying. The backdrop is, that your JS is only executed when actually studying, not when previewing the card in the "Preview"-Window or when editing a card template. This is due to the hooks you used only being called when actualy studying. I will take a look into that.

Best regards!

krassowski commented 9 years ago

Should works after last commit. Could you check it? (It's not uploaded to Anki servers yet, I want to check twice if it works well after last changes)