yoursdearboy / omegat-browser

Plugin for OmegaT for fast access to websites using Groovy scripts
15 stars 2 forks source link

Having a hard time trying to make a script #24

Open Dinir opened 1 year ago

Dinir commented 1 year ago

I want to make a custom browser script for a different page, but I don't know what to search on the web to get help to make progress on my scripts. I see it's Groovy, which runs on Java, and there are a lot of frameworks and other things that have a component named BrowserPane which makes it impossible to search what I want to do, and google results come back to the scripts hosted in this repository.

I am losing a lot of hours trying to make a single change. Can you give me and any other potential people who want to make scripts while not knowing Java at all a good starter point? Like, I just want to know what this BrowserPane exactly is. I am keep getting documentation of BrowserPane on the web that don't even have getBrowser() or loadURL() method listed.

yoursdearboy commented 1 year ago

Hi @Dinir, I'm glad that people still use the plugin, though I had no time to maintain it. I'll try to add info on writing scripts, though you still need to know some Java to write the plugin and JavaScript to interact with the webpage.

Answering your question on BrowserPane. See ScriptsRunner class to understand how the scripts executed (enable and disable methods), especially pay attention to lines below, which define classes and variables available inside scripts. https://github.com/yoursdearboy/omegat-browser/blob/bc59fa46ba835da6c16634dcf21f09b510ef4c48/src/main/java/com/yoursdearboy/omegat/plugins/browser/ScriptsRunner.java#L22-L25 The BrowserPane and Browser classes are defined in src directory, just look through them to see methods available. Though in most situations you need the next things:

// get or create the browser pane using unique key, some title and domain
def pane = BrowserPane.get(KEY, TITLE, DOMAIN)
// get the browser
browser = pane.getBrowser()

// load URL
browser.loadURL("http://google.com")

// execute some JavaScript code on the webpage
jsCode = """
alert("Hello, world!");
"""
browser().getWebEngine().executeScript(jsCode)

To run these commands when something changes (e.g. project changed or next word activated), they must be wrapped in event listeners. See Google Translate script for example. https://github.com/yoursdearboy/omegat-browser/blob/bc59fa46ba835da6c16634dcf21f09b510ef4c48/scripts/google_translate.groovy#L38-L48

After all, theses event listeners must be registered in the system https://github.com/yoursdearboy/omegat-browser/blob/bc59fa46ba835da6c16634dcf21f09b510ef4c48/scripts/google_translate.groovy#L99

And don't forget to register an event listener which will unregister event listeners when the script disabled 🤯 https://github.com/yoursdearboy/omegat-browser/blob/bc59fa46ba835da6c16634dcf21f09b510ef4c48/scripts/google_translate.groovy#L101

Use Google Translate script as a template. Just change url in DOMAIN and jsCode to interact with the website and you are ready. Feel free to ask more questions on writing scripts here.