Last active
January 12, 2023 10:57
-
-
Save Dinir/0c41067da3a1ddce7e1a1546e3e3d002 to your computer and use it in GitHub Desktop.
OmegaT Browser plugin scripts for English-Korean translation works.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.omegat.core.Core | |
import org.omegat.core.CoreEvents | |
import org.omegat.core.events.IEditorEventListener | |
import org.omegat.gui.editor.IPopupMenuConstructor | |
import org.omegat.gui.editor.SegmentBuilder | |
import org.omegat.gui.main.MainWindow | |
import javax.swing.AbstractAction | |
import javax.swing.Action | |
import javax.swing.JComponent | |
import javax.swing.JMenuItem | |
import javax.swing.JPopupMenu | |
import javax.swing.KeyStroke | |
import javax.swing.text.JTextComponent | |
import java.awt.event.ActionEvent | |
import java.awt.event.KeyEvent | |
import javafx.application.Platform | |
def FILENAME = "speller-kr.groovy" | |
def KEY = "SPELLER_KR" | |
def TITLE = "Korean Spell Checker" | |
def DOMAIN = "speller.cs.pusan.ac.kr" | |
def pane = BrowserPane.get(KEY, TITLE, DOMAIN) | |
pane.getBrowser().loadURL(DOMAIN) | |
/* Method to extract source text area element for JS */ | |
def getSourceTextAreaJS = { -> | |
"document.getElementById('text1')" | |
} | |
/* Method to trigger translation update for JS */ | |
def updateTranslationJS = { -> | |
"(document.nextInputForm ?? document.inputForm).submit()" | |
} | |
/* Method that puts text into translation area */ | |
def updateSourceText = { text -> | |
if (text == null) text = "" | |
Platform.runLater(new Runnable() { | |
@Override | |
void run() { | |
text = ScriptHelpers.escapeJavaStyleString(text) | |
String jsCode = """ | |
fShowLoadingAniPopup(350, 300, 306, 134, true); | |
var newText = fClearGarbageBlank("${text}"); | |
var srcTextArea = ${getSourceTextAreaJS}; | |
if (newText != srcTextArea.value) { | |
srcTextArea.value = newText; | |
${updateTranslationJS} | |
} | |
""" | |
pane.getBrowser().getWebEngine().executeScript(jsCode) | |
} | |
}) | |
} | |
/* Main action that puts translation and fetch the result */ | |
Action action = new AbstractAction() { | |
@Override | |
void actionPerformed(ActionEvent e) { | |
q = ScriptHelpers.prepareText(Core.getEditor().getCurrentTranslation(), null) | |
if (q == null) return | |
updateSourceText(q) | |
} | |
} | |
// FIXME: Remove menu items | |
/* Popup menu item */ | |
Core.getEditor().registerPopupMenuConstructors(1000, new IPopupMenuConstructor() { | |
@Override | |
void addItems(JPopupMenu menu, JTextComponent comp, int mousepos, boolean isInActiveEntry, | |
boolean isInActiveTranslation, final SegmentBuilder sb) { | |
if (isInActiveEntry) { | |
// menu.addSeparator() | |
JMenuItem menuItem = menu.add("Korean Spellcheck") | |
menuItem.addActionListener(action) | |
} | |
} | |
}) | |
/* Bind hotkey: Ctrl + ALT + P */ | |
MainWindow mainWindow = (MainWindow) Core.getMainWindow() | |
int COMMAND_MASK = System.getProperty("os.name").contains("OS X") ? ActionEvent.META_MASK : ActionEvent.CTRL_MASK | |
KeyStroke keystroke = KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.ALT_MASK + COMMAND_MASK) | |
def actionMapKey = "spellcheckKR" | |
mainWindow.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keystroke, actionMapKey) | |
mainWindow.getRootPane().getActionMap().put(actionMapKey, action) | |
/* Remove all this when script is disabled */ | |
def scriptsEventListener = [ | |
onAdd : {}, | |
onEnable : {}, | |
onRemove : {}, | |
onDisable: { File file -> | |
if (file.getName() == FILENAME) { | |
pane.close() | |
mainWindow.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).remove(keystroke) | |
mainWindow.getRootPane().getActionMap().remove(actionMapKey) | |
scriptsRunner.unregisterEventListener(scriptsEventListener) | |
} | |
}].asType(ScriptsEventListener) | |
scriptsRunner.registerEventListener(scriptsEventListener) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
These are just a slightly modified version of Google Search script and DeepL script.
Shortcuts are assigned, and will update the corresponding page on invocation.
Spell Checker stops updating when the last result lands in the "no errors found" page. Please manually click the button to go back so it can resume working, until I find a way to make that automated as well.
It would be great if I can make and gather some variables on top for the values needed to be changed for different sites, or even merge them into a single script hoping that it would reduce memory and battery usage by a bit. Also apply a custom css for the spell checker page which looks inhumane on a vertically long narrow area. But do I have time for this?
OmegaT
Browser plugin
The scripts work on 5.7.1.