[Userscript] Forum: IME2Furigana

Version 1.8:

Added fallback method if original method does not work

The community forum has a strict content security policy (CSP) which prevents some script managers from injecting the script into the page context. While they might fall back to injecting the script into the content context so that the script still runs, a script running in the content context cannot access the global variables in the page context (especially with the strict CSP in place).

The new version of IME2Furigana adds a fallback method that does not access any global variables. Instead of injecting the furigana markup to ruby conversion into Discourse functionality, the fallback method just applies the conversion to the input box content, lets Discourse generate the preview, and then reverts the input box content back. This method might be slower and more error-prone, but it seems to work well enough to be usable.

The furigana preview gets temporarily disabled during IME input because the IME input would get cancelled on each input box content change.

Known affected setups: Violentmonkey in Firefox, Userscripts in Safari (Tampermonkey in Safari seems to be unable to inject any scripts into the community forum at all).

If the script has to fall back to this slightly inferior method it prints a warning into the console.

F button now indicates if IME2Furigana tampers with your input

Sometimes when editing long posts without any furigana (for example when I am posting an entire userscript) I feel like I should temporarily disable IME2Furigana to make sure that it does not convert any false positives without me noticing. The new version adds a blue underline to the F button if IME2Furigana finds something that looks like furigana markup so that you get warned of the script modifying your input.

Added hotkey for changing the mode (off – on – blur)

Ctrl+Shift+F while the textbox is focused now allows you to quickly change the script mode.

Now supporting words containing 々

Additionally, I have changed the regular expressions to use Unicode property escapes based on @polv’s suggestion. /^\p{scx=Hiragana}+$/u is much easier to understand (and probably more complete) than /^[\u3041-\u3096\u3000-\u303f\uff01-\uff5eー]+$/.

You now can undo the automatic furigana markup

If you forgot to disable IME2Furigana before entering some Japanese text for which you don’t want furigana added, you can now undo the furigana markup with Ctrl+Z (does not yet work with the fallback method).

For this, I changed the code to use the deprecated document.execCommand("insertText", false, markup). It seems that it is still supported in all browsers and is also used by Discourse itself to make its own text manipulations undoable (e.g. clicking the italics button).

Please let me know if something doesn’t work.
Link to previous script version for downgrading in case version 1.8 doesn’t work for you

@MissDagger I don’t know why you sometimes get two F buttons. The console output makes me think that the userscript manager injected IME2Furigana twice for some reason. The new version now checks if the button is already there before adding it to hopefully avoid this problem.