kiwix / kiwix-js

Fully portable & lightweight ZIM reader in Javascript
https://www.kiwix.org/
GNU General Public License v3.0
302 stars 127 forks source link

Wiktionary translations can't be shown in kiwix-js #973

Closed danielzgtg closed 1 year ago

danielzgtg commented 1 year ago

Wiktionary translations can't be shown in kiwix-js. This affects both JQuery and ServiceWorker modes. This affects both Firefox and Chrome. I tested with wiktionary_en_all_maxi_2023-02.zim. It is related to https://github.com/openzim/mwoffliner/issues/1033 , but this time the issue is with kiwix-js because other viewers can display the content just fine.

Expected Behavior

They should be shown like in kiwix-android:

image

Actual Behavior

kiwix-js doesn't show them:

image

Jaifroid commented 1 year ago

Thanks for the report. This is strange. The fix upstream should have made it through to here. I'll need to implement fix for #972 before this can be properly tested.

kelson42 commented 1 year ago

Works with kiwix-serve AFAIK, really strange it does not work out-of-the-box here. Worrying!

danielzgtg commented 1 year ago

Also affects https://github.com/kiwix/kiwix-js-windows when "Use locally cached Wikimedia styles" is disabled. But that's not much of a problem as that option is instead set to enabled by default.

If I open DevTools before opening the zim, the debugger stops at uncaught exceptions. One of them is as follows:

Uncaught ReferenceError: module is not defined
    <anonymous> https://moz-extension.kiwix.org/current/wiktionary_en_all_maxi_2023-02.zim/-/mw/mediawiki.storage.js:309
    <anonymous> https://moz-extension.kiwix.org/current/wiktionary_en_all_maxi_2023-02.zim/-/mw/mediawiki.storage.js:314
mediawiki.storage.js:309:2
    <anonymous> https://moz-extension.kiwix.org/current/wiktionary_en_all_maxi_2023-02.zim/-/mw/mediawiki.storage.js:309
    <anonymous> https://moz-extension.kiwix.org/current/wiktionary_en_all_maxi_2023-02.zim/-/mw/mediawiki.storage.js:314
    mw.storage.session = new SafeStorage( sessionStorage );

    module.exports = {
        local: mw.storage,
        session: mw.storage.session
    };
Jaifroid commented 1 year ago

I've determined that this problem is caused by the failure to run the following script from the ZIM:

-/mw/ext.gadget.defaultVisibilityToggles.js

It's not immediately clear to my why this script fails to load, but there are a lot of exceptions in console, which seem to be to do with our blocking of top-frame navigation. Compare this with https://library.kiwix.org/content/wiktionary_en_all_maxi_2023-02/A/country, where top-frame navigation is not yet blocked (and the iframe/UI is destroyed).

image

danielzgtg commented 1 year ago

which seem to be to do with our blocking of top-frame navigation

I thought that causes the ZIM's Javascript to silently ignore the navigation attempt. I don't think it is an exception that stops the rest of the script from executing because I don't see a stack trace in your screenshot.

I think there problem is related to the module and mw variables not being set for some reason.

danielzgtg commented 1 year ago

Nvm, I reproduced it with

// test1.html
<iframe src="./test2.html" sandbox="allow-same-origin allow-scripts"></iframe>
// test2.html
<script>
window.top.location = "https://example.org";
console.log("It still works!");
</script>
// python3 -m http.server

I get a DOMException in this example. I looked at the kiwix-js logs, and I still don't see a DOMException for some reason.

Fixing this might require ZIM-specific stuff. See https://github.com/openzim/mwoffliner/issues/1803#issuecomment-1454685147 for what I tried and failed to do with Object.freeze and related.

Jaifroid commented 1 year ago

Thanks! I note that in startup.js (a file that runs on every article page load in Wiktionary), the file mediawiki.page.ready.js (which causes the navigation) is loaded before defaultVisibilityToggles.js. When the navigation is blocked, an exception occurs which bubbles up to startup.js. I'm not 100% sure of the sequencing, but what is sure is that defaultVisibilityToggles.js is never loaded, and so the dynamic open/close buttons are never added to the translation div heads.

image

I could temporarily patch this by removing the client-js class from the html (dynamically, after page load), which opens all the translation blocks... Pending an upstream fix in https://github.com/openzim/mwoffliner/issues/1803.

Jaifroid commented 1 year ago

Removing this class in devTools causes all translations to be displayed:

image

NB In jQuery mode, the client-js class is moved to the <body> tag of the article document for technical reasons to do with how the document is injected in the iframe.

Jaifroid commented 1 year ago

It seems to be working in the March Spanish Wiktionary and the April Portuguese Wiktionary. We don't yet have a new English Wiktionary to test. But it looks like it's now fixed in the ZIMs. The February English Wiktionary probably hadn't received the mwOffliner fix when it was scraped.

image image

Jaifroid commented 1 year ago

I'm closing, but please re-open if it turns out not to be fixed when we get a new English Wiktionary.