[Userscript] Keisei 形声 Semantic-Phonetic Composition

Alright, that makes things easier on me because we should have the same setup (aside from maybe OS but that shouldn’t factor in here).

Have you tried restarting the browser? If you need to do this without losing open tabs, you can type about:profiles in the url bar and then you should see (aside from other things) the option to “Restart normally” which will perform a restart without losing open tabs/windows

1 Like

I am still getting the same error

1 Like

What other scripts do you have installed by the way? Maybe that can help me.

In the meantime, I think I am going to try to whip up a quick test version of the script for you to install that will help me troubleshoot more. More info on that when I have it.

For all that I can see, you should not be having this problem. The Icons class is properly being added to the file, which means the line in the library script that sets it on the window should be executing without issue. The fact that it is not getting set on the window may not be an issue with any code written by this script or that library script, which would be the hardest to figure out.

To anyone else, please also comment if you are getting the same error so I can determine if this is just an issue for noelnimstad or not.

1 Like

I’ve tried turning everything other script off, but that didn’t work either.

1 Like

Ok, good news, I was able to reproduce the problem.

Less good news, it’s gonna take me a day or so to work out the fix. The problem is something inherent in how Keisei (and Niai) are structured, with lots of closures. This means pretty much every function where I try to access the window object is affected and I need to figure out the best approach with the script’s structure. Thank you for the report. I suspect a lot won’t run into the issue because they use at least one other script that uses Custom Icons and doesn’t have this weird structure which sets Icons on the window which Keisei (and Niai) can then access like any other script.

1 Like

A quick hotfix is to do the following:

Go into your script manager, create a new script and replace the contents with

// ==UserScript==
// @name         TEMP - Load Custom Icons for WaniKani
// @namespace    http://tampermonkey.net/
// @version      2024-06-08
// @description  a temporary fix for the Keisei and Niai scripts
// @author       You
// @match        https://www.wanikani.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=wanikani.com
// @require      https://greasyfork.org/scripts/489759-wk-custom-icons/code/CustomIcons.js?version=1386034
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    let Icons = window.Icons;

    try {
        console.log(`CustomIcons: ${Icons.SCRIPT_VER}`);
    }
    catch (err) {
        console.error(`Could not fetch CustomIcons`);
    }
})();

Just by having this script run (it can even be put after Keisei/Niai) the latter scripts should work now.

1 Like

This works; thankyou!

1 Like

Right, the problem with Keisei and Niai is that they use a few @grants, which causes these scripts to run in sandbox mode, meaning that their window is sandboxed. Since the Custom Icons Library script adds Icons to the sandboxed window, it is not available outside of the sandbox. Your code, however, tries to get Icons from the unsafeWindow, which is outside of the sandbox:

const { Icons } = window.unsafeWindow || window;

I don’t know exactly how the Custom Icons Library script works, but maybe it is enough if you instead write something like this?

const Icons = window.unsafeWindow?.Icons ?? window.Icons;

Alternatively, @Hubbit200 could change Custom Icons Library to store Icons not in window, but in (window.unsafeWindow ?? window), so that even a sandboxed userscript installs the Icons library globally (outside of the sandbox).

1 Like

Thanks! This was exactly the issue and switching to your suggestion fixes it. PR is made now.

2 Likes

The script doesnt seem to be working rn on individual kanji pages or lessons

Ok, my previous assessment was off because it turned out I had the script disabled. Once I enabled it, I verified that the latest version (1.8.40) is working for me. Can you give me the link to a specific kanji page you observed it not working on? Or which kanji during lessons you saw it not working.

I have version 1.8.40
It is not showing on ANY kanji pages or lessons such as https://www.wanikani.com/kanji/張

This is the error I see

I also tried disabling all other userscripts, it still does not fix the issue

Do you have Open Framework? It’s needed for JQuery as Wanikani has been removing JQuery from pages across the site (I believe the dashboard is the only one left that has it provided by WK).

I enabled open framework then went through enabling every other script I had installed one by one. I found the culprit. It was this script, which I had installed directly before it was converted into a library

Gotcha. Yes, as it has been made into a library you shouldn’t have it installed locally anymore. Hope everything is working as intended now.

1 Like