Skip to content

Commit d9f03e9

Browse files
authored
Merge pull request #1 from phw/gm-xmlhttprequest
Implement support for GM.xmlHttpRequest
2 parents 10c41e5 + 8d8b503 commit d9f03e9

File tree

3 files changed

+39
-19
lines changed

3 files changed

+39
-19
lines changed

.eslintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"env": {
33
"browser": true,
4-
"es2017": true
4+
"es2017": true,
5+
"greasemonkey": true
56
},
67
"extends": [
78
"standard"

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ Some things to be aware off:
3838
using Greasemonkey, Violentmonkey and Tampermonkey. It might or might not work on other browsers or
3939
with other user script managers. The code assumes your browser supports modern JS features (at
4040
least ES2017).
41-
- The user script does not work with Safari due to strict blocking of unencrypted connections even if
42-
they are local.
41+
- For Safari you need the [Userscripts](https://github.com/quoid/userscripts) extension version 4
42+
(which requires macOS 12.0+) and version 0.7 of MusicBrainz Magic Tagger Button.
43+
*The user script does not work in Safari with older versions of the Userscripts extension due
44+
to strict blocking of unencrypted connections even if they are local.*
4345
- I still consider this experimental and a proof of concept. It works for me, but might not for you.
4446
- It is recommended that you have configured Picard to use the default port 8000, but it has to be
4547
one port between 8000 - 8010.
@@ -56,6 +58,6 @@ Some things to be aware off:
5658

5759
## License
5860

59-
MusicBrainz Magic Tagger Button © 2021 Philipp Wolfer <[email protected]>
61+
MusicBrainz Magic Tagger Button © 2021-2022 Philipp Wolfer <[email protected]>
6062

6163
Published under the MIT license, see [LICENSE](./LICENSE) for details.

mb-magic-tagger-button.user.js

+32-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ==UserScript==
22
// @name MusicBrainz Magic Tagger Button
33
// @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
55
// @author Philipp Wolfer
66
// @namespace https://uploadedlobster.com
77
// @license MIT
@@ -21,7 +21,7 @@
2121
// @exclude /^https://([.*].)?musicbrainz.org/collection/.*/.*/
2222
// @exclude /^https://([.*].)?musicbrainz.org/release-group/.*/.*/
2323
// @exclude /^https://([.*].)?musicbrainz.org/series/.*/.*/
24-
// @grant none
24+
// @grant GM.xmlHttpRequest
2525
// @inject-into content
2626
// @noframes
2727
// @homepageURL https://github.com/phw/musicbrainz-magic-tagger-button
@@ -85,28 +85,45 @@ const log = (...args) => logger('log', ...args)
8585
const warn = (...args) => logger('warn', ...args)
8686
const error = (...args) => logger('error', ...args)
8787

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+
88104
function makeRequest (method, url) {
89105
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) => {
94107
resolve({
95108
method: method,
96109
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,
101113
})
102114
}
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}`
105117
reject(new Error(msg))
106118
}
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+
})
110127
})
111128
}
112129

0 commit comments

Comments
 (0)