|
1 | 1 | // ==UserScript==
|
2 | 2 | // @name MusicBrainz Magic Tagger Button
|
3 | 3 | // @description Automatically enable the green tagger button on MusicBrainz.org depending on whether Picard is running.
|
4 |
| -// @version 0.6.3 |
| 4 | +// @version 0.7 |
5 | 5 | // @author Philipp Wolfer
|
6 | 6 | // @namespace https://uploadedlobster.com
|
7 | 7 | // @license MIT
|
|
21 | 21 | // @exclude /^https://([.*].)?musicbrainz.org/collection/.*/.*/
|
22 | 22 | // @exclude /^https://([.*].)?musicbrainz.org/release-group/.*/.*/
|
23 | 23 | // @exclude /^https://([.*].)?musicbrainz.org/series/.*/.*/
|
24 |
| -// @grant none |
| 24 | +// @grant GM.xmlHttpRequest |
25 | 25 | // @inject-into content
|
26 | 26 | // @noframes
|
27 | 27 | // @homepageURL https://github.com/phw/musicbrainz-magic-tagger-button
|
@@ -85,28 +85,45 @@ const log = (...args) => logger('log', ...args)
|
85 | 85 | const warn = (...args) => logger('warn', ...args)
|
86 | 86 | const error = (...args) => logger('error', ...args)
|
87 | 87 |
|
| 88 | +let xmlHttpRequest |
| 89 | +if (typeof (GM) !== 'undefined' && GM.xmlHttpRequest) { |
| 90 | + debug('Using GM.xmlHttpRequest') |
| 91 | + xmlHttpRequest = GM.xmlHttpRequest |
| 92 | +} else { |
| 93 | + debug('Using XMLHttpRequest') |
| 94 | + xmlHttpRequest = function xmlHttpRequest (details) { |
| 95 | + const xhr = new XMLHttpRequest() |
| 96 | + xhr.timeout = details.timeout || 0 |
| 97 | + xhr.open(details.method, details.url) |
| 98 | + xhr.onload = details.onload.bind(null, xhr) |
| 99 | + xhr.onerror = details.onerror.bind(null, xhr) |
| 100 | + xhr.send() |
| 101 | + } |
| 102 | +} |
| 103 | + |
88 | 104 | function makeRequest (method, url) {
|
89 | 105 | return new Promise((resolve, reject) => {
|
90 |
| - const xhr = new XMLHttpRequest() |
91 |
| - xhr.timeout = 200 |
92 |
| - xhr.open(method, url) |
93 |
| - xhr.onload = () => { |
| 106 | + const successHandler = (response) => { |
94 | 107 | resolve({
|
95 | 108 | method: method,
|
96 | 109 | url: url,
|
97 |
| - status: xhr.status, |
98 |
| - statusText: xhr.statusText, |
99 |
| - response: xhr.response, |
100 |
| - responseText: xhr.responseText, |
| 110 | + status: response.status, |
| 111 | + statusText: response.statusText, |
| 112 | + responseText: response.responseText, |
101 | 113 | })
|
102 | 114 | }
|
103 |
| - const errorHandler = () => { |
104 |
| - const msg = `Request failed ${method} ${url}: ${xhr.status} ${xhr.statusText}` |
| 115 | + const errorHandler = (response) => { |
| 116 | + const msg = `Request failed ${method} ${url}: ${response.status} ${response.statusText}` |
105 | 117 | reject(new Error(msg))
|
106 | 118 | }
|
107 |
| - xhr.onerror = errorHandler |
108 |
| - xhr.ontimeout = errorHandler |
109 |
| - xhr.send() |
| 119 | + xmlHttpRequest({ |
| 120 | + method: method, |
| 121 | + url: url, |
| 122 | + timeout: 200, |
| 123 | + onload: successHandler, |
| 124 | + onerror: errorHandler, |
| 125 | + ontimeout: errorHandler, |
| 126 | + }) |
110 | 127 | })
|
111 | 128 | }
|
112 | 129 |
|
|
0 commit comments