Skip to content

Instantly share code, notes, and snippets.

@Dinir
Last active January 12, 2023 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dinir/0c41067da3a1ddce7e1a1546e3e3d002 to your computer and use it in GitHub Desktop.
Save Dinir/0c41067da3a1ddce7e1a1546e3e3d002 to your computer and use it in GitHub Desktop.
OmegaT Browser plugin scripts for English-Korean translation works.
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
def FILENAME = "naver-enkr.groovy"
def KEY = "NAVER_ENKR"
def TITLE = "Naver EN-KR Dict."
def DOMAIN = "en.dict.naver.com"
def URL = "en.dict.naver.com/#/mini"
def pane = BrowserPane.get(KEY, TITLE, DOMAIN)
pane.getBrowser().loadURL(URL + "/main")
/* Record word at caret */
String caretWord = null
def editorEventListener = new IEditorEventListener() {
@Override
void onNewWord(String s) {
caretWord = s
}
}
CoreEvents.registerEditorEventListener(editorEventListener)
/* Main action that opens the site */
Action action = new AbstractAction() {
@Override
void actionPerformed(ActionEvent e) {
q = ScriptHelpers.prepareText(Core.getEditor().getSelectedText(), caretWord)
if (q == null) return
url = "https://${URL}/search?"
url += "query=${ScriptHelpers.encodeText(q)}"
pane.getBrowser().loadURL(url)
}
}
// 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("Search in Naver EN-KR Dictionary")
menuItem.addActionListener(action)
}
}
})
/* Bind hotkey: Ctrl + ALT + E */
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_E, ActionEvent.ALT_MASK + COMMAND_MASK)
def actionMapKey = "searchInNaverENKR"
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()
CoreEvents.unregisterEditorEventListener(editorEventListener)
mainWindow.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).remove(keystroke)
mainWindow.getRootPane().getActionMap().remove(actionMapKey)
scriptsRunner.unregisterEventListener(scriptsEventListener)
}
}].asType(ScriptsEventListener)
scriptsRunner.registerEventListener(scriptsEventListener)
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
def FILENAME = "naver-kr.groovy"
def KEY = "NAVER_KR"
def TITLE = "Naver Korean Dict."
def DOMAIN = "ko.dict.naver.com"
def URL = "ko.dict.naver.com/#/mini"
def pane = BrowserPane.get(KEY, TITLE, DOMAIN)
pane.getBrowser().loadURL(URL + "/main")
/* Record word at caret */
String caretWord = null
def editorEventListener = new IEditorEventListener() {
@Override
void onNewWord(String s) {
caretWord = s
}
}
CoreEvents.registerEditorEventListener(editorEventListener)
/* Main action that opens the site */
Action action = new AbstractAction() {
@Override
void actionPerformed(ActionEvent e) {
q = ScriptHelpers.prepareText(Core.getEditor().getSelectedText(), caretWord)
if (q == null) return
url = "https://${URL}/search?"
url += "query=${ScriptHelpers.encodeText(q)}"
pane.getBrowser().loadURL(url)
}
}
// 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("Search in Naver Korean Dictionary")
menuItem.addActionListener(action)
}
}
})
/* Bind hotkey: Ctrl + ALT + K */
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_K, ActionEvent.ALT_MASK + COMMAND_MASK)
def actionMapKey = "searchInNaverKR"
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()
CoreEvents.unregisterEditorEventListener(editorEventListener)
mainWindow.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).remove(keystroke)
mainWindow.getRootPane().getActionMap().remove(actionMapKey)
scriptsRunner.unregisterEventListener(scriptsEventListener)
}
}].asType(ScriptsEventListener)
scriptsRunner.registerEventListener(scriptsEventListener)
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)
@Dinir
Copy link
Author

Dinir commented Jan 10, 2023

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.

Ctrl+Alt+ Updating Page Text
E Naver En-Kr Dictionary words at carat or selection
K Naver Korean Dictionary words at carat or selection
P Pusan Spell Checker translation for current segment

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment