xiaoyifang / goldendict-ng

The Next Generation GoldenDict
https://xiaoyifang.github.io/goldendict-ng/
Other
1.62k stars 88 forks source link

Add script plug-in mechanism #1332

Open popyoung opened 9 months ago

popyoung commented 9 months ago

Is your feature request related to a problem? Please describe. 之前提过,现在和anki的通讯不是很稳定。而且功能上可定制化不强。所以是不是考虑用脚本插件的形式来解决这个问题。而且修改一些和anki协议相关的问题就不需要重新编译,改改脚本就可以了。而且也可以完成一些其他的定制化需求,比如接入其他的类似软件,导出excel什么的。

Describe the solution you'd like 插件语言的话JS或者python都可以。python更流行,js提取html更方便些,都不需要第三方库。 初步是设想右键菜单里的按钮可以用脚本实现。传入参数就是html(python的话传入html的文本,插件维护者自己用自己熟悉的第三方库来解析)以及选中的文本和位置。(选中的词典名可能也需要,虽然也可以解析出来,但稍微有点麻烦) 之后甚至可以考虑加入其他事件。鼠标、键盘啥的。 插件配置初期简单点就直接改ini或者json,理想情况是也有个UI专门配置插件,比如anki的插件就有两个位置可以配置,一个是插件中心(管理全局功能),一个是deck option(管理对应deck功能)。UI这块可能比较复杂,感觉可以先跳过。

所以综合下来初期版本就是一个鼠标右键事件中传入一些必要参数的脚本接入。后期再考虑其他事件接入以及UI问题。

github-actions[bot] commented 9 months ago

Bot detected the issue body's language is not English, translate it automatically.

Is your feature request related to a problem? Please describe. As mentioned before, the current communication with anki is not very stable. And the functionality is not very customizable. So should you consider using a script plug-in to solve this problem? And to modify some issues related to the Anki protocol, you don't need to recompile, just change the script. And it can also complete some other customized requirements, such as connecting to other similar software, exporting excel, etc.

Describe the solution you'd like As for the plug-in language, JS or python can be used. Python is more popular, and js is more convenient to extract html, and no third-party library is needed. The initial idea is to imagine that the buttons in the right-click menu can be implemented using scripts. The incoming parameters are html (in python, the text of html is passed in, and the plug-in maintainer uses a third-party library he is familiar with to parse it) and the selected text and position. (The selected dictionary name may also be needed. Although it can also be parsed, it is a little troublesome) You might even consider adding other events later. Mouse, keyboard, etc. In the early stage of plug-in configuration, it is simple to directly change ini or json. Ideally, there is also a UI to specifically configure the plug-in. For example, Anki plug-in has two places to configure, one is the plug-in center (manage global functions), and the other is deck option (manage Corresponding to the deck function). The UI may be complicated, so I feel like I can skip it first.

So in summary, the initial version is a script access that passes in some necessary parameters in the right mouse button event. We will consider other event access and UI issues later.

xiaoyifang commented 9 months ago

是个好想法,不是很好执行。

github-actions[bot] commented 9 months ago

Bot detected the issue body's language is not English, translate it automatically.

It's a good idea, but not very well executed.

popyoung commented 9 months ago

js集成感觉很简单,和现在的用户脚本差不多。约定好插件js实现一个指定名称的函数就可以了。比如实现

function ButtonClicked (dictName)

因为是用户脚本这个域,document是通用的都不用传入,类似getSelection()也能获得用户选中的内容。 几乎是只约定一个函数名称,就能打通流程。反正就是用户点击那个右键菜单里的按钮,程序就调用那段ButtonClicked(我没去看右键菜单是怎么实现的。但是有词典跳转功能和anki相关功能,想来应该是可以和html交互的) 至于按钮的文字,可以约定json或ini,也可以读取约定好的js变量名称。

py集成,估计复杂点。可以参考官网的https://docs.python.org/3/extending/extending.html

github-actions[bot] commented 9 months ago

Bot detected the issue body's language is not English, translate it automatically.

js integration feels very simple, almost the same as current user scripts. Just agree on the plug-in js to implement a function with a specified name. Such as realizing

functionButtonClicked(dictName)

Because it is a user script domain, the document is universal and does not need to be passed in. Similar to getSelection(), the content selected by the user can also be obtained. Almost just agreeing on a function name can open up the process. Anyway, when the user clicks the button in the right-click menu, the program calls the ButtonClicked section (I didn't see how the right-click menu is implemented. But there are dictionary jump functions and anki related functions, and I think it should be able to interact with HTML) As for the text of the button, you can agree on json or ini, or you can read the agreed js variable name.

py integration, estimate the complexity. You can refer to the official website https://docs.python.org/3/extending/extending.html