N
N
nirvimel2017-01-06 18:57:53
JavaScript
nirvimel, 2017-01-06 18:57:53

How to properly implement modules in userscripts (userscript)?

I did not find any information on the Web about how userscript projects should be developed. Obviously, any project larger than HelloWorld should have a modular structure internally, especially if the same modules are used in many different projects and at the same time are themselves (before) developed.
https://wiki.greasespot.net/Third-Party_Libraries - all this is not the same, it only applies to third-party libraries that are already on the Web, but not modules of their own project in development.
I just started writing a small automation of actions on one site and I already have a bunch of scripts, consisting of 90% of the general code, which I modify somewhere, I forget to copy the updated one somewhere ... in general, before I start, I already fell into the hell of copy-pasting, where all violators of the DRY principle fall.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Александр Аксентьев, 2017-01-06
@nirvimel

Это же обычное JS приложение. Соответственно работать как с JS.
Любой фреймворк использовать и т.п.
Структура соответствующая.

D
Dmitry, 2020-01-20
@b00

Move the general code to the "library", for example, in the form:

window.unsafeWindow = window.unsafeWindow || window;
const w = unsafeWindow;
w.Lib = {
 Foo: function(){},
 ...
}
w.Lib.Bar = {
  ...
}

etc. And load it through the directive // @run-at document-start
. Things dependent on the common code - load through // @run-at document-end
Example: https://github.com/dimio/userscripts-spacom.ru-addons

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question