|
1 | 1 | // ==UserScript== |
2 | 2 | // @name GM.runExclusive Demo |
3 | 3 | // @namespace https://docs.scriptcat.org/ |
4 | | -// @version 0.1.0 |
| 4 | +// @version 0.1.1 |
5 | 5 | // @match https://example.com/*?runExclusive* |
6 | 6 | // @grant GM.runExclusive |
7 | 7 | // @grant GM.setValue |
|
129 | 129 |
|
130 | 130 | const startWait = performance.now(); |
131 | 131 |
|
132 | | - const result = await GM.runExclusive('demo-lock-key', async () => { |
133 | | - const waited = Math.round(performance.now() - startWait); |
| 132 | + try { |
| 133 | + const result = await GM.runExclusive('demo-lock-key', async () => { |
| 134 | + const waited = Math.round(performance.now() - startWait); |
134 | 135 |
|
135 | | - const order = (await GM.getValue('order')) + 1; |
136 | | - await GM.setValue('order', order); |
| 136 | + const order = (await GM.getValue('order')) + 1; |
| 137 | + await GM.setValue('order', order); |
137 | 138 |
|
138 | | - log(`Lock acquired (#${order}, waited ${waited}ms)`, '#0f0'); |
| 139 | + log(`Lock acquired (#${order}, waited ${waited}ms)`, '#0f0'); |
139 | 140 |
|
140 | | - const val = await GM.getValue('mValue01'); |
141 | | - await GM.setValue('mValue01', val + timeDelay); |
| 141 | + const val = await GM.getValue('mValue01'); |
| 142 | + await GM.setValue('mValue01', val + timeDelay); |
142 | 143 |
|
143 | | - log(`Working ${timeDelay}ms…`, '#ff0'); |
144 | | - await new Promise(r => setTimeout(r, timeDelay)); |
| 144 | + log(`Working ${timeDelay}ms…`, '#ff0'); |
| 145 | + await new Promise(r => setTimeout(r, timeDelay)); |
145 | 146 |
|
146 | | - const final = await GM.getValue('mValue01'); |
147 | | - log(`Done. Shared value = ${final}`, '#f55'); |
| 147 | + const final = await GM.getValue('mValue01'); |
| 148 | + log(`Done. Shared value = ${final}`, '#f55'); |
148 | 149 |
|
149 | | - return { order, waited, final }; |
150 | | - }); |
| 150 | + return { order, waited, final }; |
| 151 | + }); |
| 152 | + log(`Result: ${JSON.stringify(result)}`, '#fff'); |
| 153 | + } catch (e) { |
| 154 | + log(`Error: ${JSON.stringify(e?.message || e)}`, '#f55'); |
| 155 | + } |
151 | 156 |
|
152 | | - log(`Result: ${JSON.stringify(result)}`, '#fff'); |
153 | 157 |
|
154 | 158 | })(); |
0 commit comments