Skip to content

Commit e3667c7

Browse files
committed
Update gm_run_exclusive.js
1 parent fd301c4 commit e3667c7

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

example/gm_run_exclusive.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ==UserScript==
22
// @name GM.runExclusive Demo
33
// @namespace https://docs.scriptcat.org/
4-
// @version 0.1.0
4+
// @version 0.1.1
55
// @match https://example.com/*?runExclusive*
66
// @grant GM.runExclusive
77
// @grant GM.setValue
@@ -129,26 +129,30 @@
129129

130130
const startWait = performance.now();
131131

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);
134135

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);
137138

138-
log(`Lock acquired (#${order}, waited ${waited}ms)`, '#0f0');
139+
log(`Lock acquired (#${order}, waited ${waited}ms)`, '#0f0');
139140

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);
142143

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));
145146

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');
148149

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+
}
151156

152-
log(`Result: ${JSON.stringify(result)}`, '#fff');
153157

154158
})();

0 commit comments

Comments
 (0)