Skip to content

Commit 246d096

Browse files
committed
doc: modernize fav editor guide
1 parent 7e2e8cd commit 246d096

File tree

4 files changed

+41
-27
lines changed

4 files changed

+41
-27
lines changed
-19.5 KB
Loading
80 Bytes
Loading

content/posts/how-to-edit-scripts-with-your-favorite-editor/index.md

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,61 +12,75 @@ redirect_from:
1212
- /2017/03/14/How-to-edit-scripts-with-your-favorite-editor/
1313
---
1414

15-
For anyone who uses Violentmonkey, it is easy to find a way to edit a script like this:
15+
Since Violentmonkey is a lightweight script manager by design, its editor is simple and offers just a few options. It doesn't try to replicate a full-featured editor or IDE, which is problematic anyway due to browser's hardcoded hotkeys and other restrictions.
16+
17+
<details>
18+
<summary>Screenshot of the built-in editor and its options</summary>
1619

1720
![](editor-1.png)
1821

19-
However, it is really hard to make everyone love it. *A* probably likes a theme of `monokai`, while *B* prefers `eclipse`. *C* might use 2 spaces for indentation while *D* prefers tabs.
22+
Customization in the extension's settings:
2023

21-
It is impossible to satisfy every single user. Lots of configurations may be added for this, but are they really worth it? A user would have to face lots of choices before he could use it just as a script manager, or have to look for a simple option among lots of unrelated editor options. What is worse, the browser may inhibit some shortcuts or operations that a local editor can have. In other words, an editor inside Violentmonkey will never be as good as your favorite one.
24+
![](editor-2.png)
2225

23-
Here comes the question: How to edit my script with my favorite editor?
26+
</details>
2427

25-
Save to a file
26-
---
28+
<br/>
2729

28-
First, copy the script to your favorite editor and save it to a file.
30+
Excellent external editors exist and using one to edit scripts in Violentmonkey is relatively simple, no need to reinvent the wheel.
2931

30-
![](editor-2.png)
32+
## 1. One-time preparation
3133

32-
VIM is my favorite editor, and the script is saved at `D:/Source/vm-script.user.js`. Note that the script file must have a name ending with `.user.js`, otherwise won't be recognized by Violentmonkey.
34+
1. Copy the code of the script from Violentmonkey into your editor.
35+
2. Save it to a file and give it a name ending with `.user.js`
3336

34-
Install a local script
35-
---
37+
## 2. Open this file in Violentmonkey installer
38+
39+
⚠ The local file's name must end with `.user.js`, otherwise it won't be recognized by Violentmonkey. The only exception is method 2.1, which recognizes any `.js` file because you explicitly drag it into the extension.
3640

37-
First make sure the local script is named with a suffix of `.user.js`.
41+
You can use any of these methods listed in the order of increasing complexity:
3842

39-
Then use one of these methods:
43+
#### 2.1. Drag the file into any Violentmonkey tab or popup
4044

41-
- Drag the file into any Violentmonkey tab or popup.
45+
⚠ Only implemented in Chrome/Chromium 86 or newer and Violentmonkey 2.16 or newer.
4246

43-
Requires Chrome/Chromium 86+ and Violentmonkey 2.16+.
47+
⚡ If you assigned a hotkey to activate Violentmonkey in the browser's options for extensions, you can press it while dragging the file, which opens the extension's popup into which you can drop the dragged file.
4448

45-
- Drag the file into the browser toolbar or tab label area.
49+
#### 2.2. Drag the file into the browser toolbar or tab label area
4650

4751
* In Chrome-like browsers you must enable *"Allow access to file URLs"* in Violentmonkey's details in `chrome://extensions` page first. Since this is dangerous (any userscript will be able to read any local file via GM_xmlhttpRequest), you may want to use the other methods instead.
4852

49-
* In modern Firefox-like browsers don't close the file tab while tracking as it's used to read the contents of the file.
53+
* In modern Firefox-like browsers don't close the tab of this file while tracking as it's used to read the contents of the file.
5054

51-
- Set up a server and navigate to `http://localhost:8080/my-script.user.js`.
55+
#### 2.3. Set up a server and navigate to `http://localhost:8080/my-script.user.js`
5256

5357
The port number `:8080` may be different or even omitted depending on the server you installed.
5458

55-
We recommend [indexzero/http-server](https://github.com/indexzero/http-server#readme). Once you install it, just type `http-server -c5` at the directory of your script to start a server. The `-c5` option sets cache time for max-age header to 5 seconds ([more info](https://github.com/violentmonkey/violentmonkey/issues/460#issuecomment-434335758)), thus forcing a browser to query the server URL every 5 seconds. The caching can be disabled with `-c-1` option.
59+
A good server is [indexzero/http-server](https://github.com/indexzero/http-server#readme). Once you install it, just type `http-server -c5` at the directory of your script to start a server. The `-c5` option sets cache time for max-age header to 5 seconds ([more info](https://github.com/violentmonkey/violentmonkey/issues/460#issuecomment-434335758)), thus forcing a browser to query the server URL every 5 seconds. The caching can be disabled with `-c-1` option.
60+
61+
If you want a UI, there are such servers as well, e.g. `HTTP File Server` for Windows.
5662

57-
Make sure the hostname is `localhost` and the script name ends with `.user.js`.
63+
⚠ Make sure the hostname is `localhost` and the script name ends with `.user.js`.
64+
65+
## 3. Enable tracking
5866

5967
![](editor-3.png)
6068

61-
Click `Track external edits` button to confirm installation. You can also make it the default button for local files by ticking the checkbox on the right.
69+
Click `Track external edits` button to confirm installation and start tracking.
6270

63-
Edit and sync
64-
---
71+
You can also make it the default button for local files by ticking the checkbox on the right, so it can be invoked via the hotkey shown in parentheses.
72+
73+
## 4. Edit the local file and save it
6574

66-
After installation, the confirmation page will keep watching the file before the page is closed. Once the file is changed, the new version will be installed automatically. As a result, you edit the script in your favorite editor, and the changes are synced to Violentmonkey immediately.
75+
As long as the installer tab is open it will track your local file and install the new version automatically into Violentmonkey after you save the file. You can save it either manually or by configuring auto-save in the options of your editor.
76+
77+
* The `Stop tracking` button may be useful for example when temporarily switching branches in your git repo. When finished, you can click `Track external edits` again.
78+
79+
* The `Reload tab` option allows you to switch to another tab with a website [matched](/api/matching/) by this script, so that this tab will be reloaded automatically when changes are detected and this script still matches the tab's URL.
6780

6881
![](editor-4.png)
6982

7083
Known issues
7184
---
72-
- The older the file is, the more seldom a browser will query it. See [this Violentmonkey's issue](https://github.com/violentmonkey/violentmonkey/issues/460#issuecomment-434335758).
85+
86+
- Firefox may throttle detection: the older the file is, the longer it takes to detect the changes. See [this report](https://github.com/violentmonkey/violentmonkey/issues/460#issuecomment-434335758).

src/components/toc/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default function TOC(props) {
3232
// `tableOfContents` from MDX does not support inline markups,
3333
// as a result the text content may be truncated.
3434
if (articleRef.current) {
35-
const h = articleRef.current.querySelector(`#${id}`);
35+
const h = articleRef.current.querySelector(`#${CSS.escape(id)}`);
3636
if (h) a.textContent = h.textContent.trim();
3737
}
3838
});
@@ -42,7 +42,7 @@ export default function TOC(props) {
4242
const headings = list
4343
.map((a) => {
4444
const { id } = a.dataset;
45-
const el = articleRef.current.querySelector(`#${id}`);
45+
const el = articleRef.current.querySelector(`#${CSS.escape(id)}`);
4646
return (
4747
el && {
4848
id,

0 commit comments

Comments
 (0)