[Userscript] Forum: IME2Furigana

I saw it again (a few days ago and forgot to post it here) and opened up the console to check what was there. Not sure it actually helps you, but I’ll post it anyway.

1 Like

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.

3 Likes

Yeah, I was thinking it was something like that. Nice to be able to have the standard version now.

Even if I couldn’t figure out how to get the script to install automatically to Userscrips, so I guess I’ll probably have to manually update it. :slightly_frowning_face: But that is a problem for me to solve, but I don’t care enough to figure out how to fix it because I only use Userscripts for this script. ^^

1 Like

Click “Raw” button, to add to TamperMonkey

Modification of the original, but less extra features than version 1.7.

  • Ability to render Furigana markup without auto-IME. (IMO, auto-IME is buggy, anyway.)
  • Convert back to markup only if ruby tags are originally rendered by IME2Furigana. [1]
    • This also allows complex ruby tags, such as staggering ones, e.g. ウエディング結婚けっこんリングゆび
  • Block conversion with Zero-width nonbreaking space (\u2060), or Zero-width space (\u200b).
    • Copy-paste the hidden character from the link, and paste it just after < for <⁠結婚>[けっこん] to HTML conversion, and after <ruby> tag for from HTML conversion. [2]
  • Tested on mobile Android (Kiwi Browser and Violentmonkey)
  • Removed features
    • Code block detection. (Use Zero-width nonbreaking space instead.)
    • Additional buttons and addtional stylings
      • These are just work to do, and subsequent extra maintenance.

  1. which is <ruby lang = 'ja-JP'> ↩︎

  2. <ruby lang = 'ja-JP'>⁠結婚<rt>けっこん</rt></ruby> ↩︎

2 Likes

it doesn’t really work…
the F doesn’t appear.

Can you go through the suggested steps in this guide to gather more information about the problem?

Tried all the steps…

  1. Safari
  2. Tampermonkey
  3. macOS Ventura
  4. [Userscript] Forum: IME2Furigana - #147 by polv
  5. Not really applicable
  6. All those steps

1 Like

since you are also using Tampermonkey on Safari, I am afraid this is the same problem that @MissDagger had last year. We spent a long time debugging, but in the end it turned out that the Safari-Version of Tampermonkey does not manage to inject the script appropriately on the forum:

I think the Userscripts extension works (at least it did last year).

2 Likes

aw what a shame
I’ll try your user scripts extension

It’s not mine – it’s just an alternative to Tampermonkey :smiley:

1 Like

Screenshot 2023-04-20 at 21.17.00
Do you know why I can’t get this triangle to go away

You need to allow the extension for each page (or all pages if you don’t mind it), and even after allowing it, it sometimes didn’t want to acknowledge that I’d allowed it until I restarted Safari. Userscripts is a tiny bit more finicky to get it to work on a page, but once it accepts your allowance of it working, I had no more trouble with it.

3 Likes

After tinkering around with userscripts, I got it it in the end, but it’s actually an F with a grey box?
Screenshot 2023-04-20 at 21.23.20

1 Like

When it’s activated, the button has a gray background. You can click the button to cycle through the three available modes.

2 Likes

thank you so much for helping me!

1 Like

@Sinyaven

If you ever update this script in the future, please consider adding an option to position the furigana below the kanji.

Today I discovered this is WAY easier than I expected: one line of CSS with astonishingly good cross-browser support!

In this case you would just need to add an inline style to the ruby conversion:

<ruby style="ruby-position: under;">
  <rb>日本語</rb>
  <rp>(</rp><rt>にほんご</rt><rp>)</rp>
</ruby>
2 Likes

It doesn’t work in Discourse, because style attributes get sanitized. (Similar to how you can’t put in colored text other than using LaTeX )

Injecting CSS via Userscript or Userstyles is possible, however.

3 Likes

Darn. Good catch.

A user style sheet makes it a reader’s preference rather than the author’s preference, which is better. Unfortunately it also requires every user that wants it to install stylus or whatever.

1 Like

IME2Furigana stops working today. (v1.8, Brave Browser on Windows.) Maybe Discourse update something?

3 Likes

Also not working on Google Chrome either.