XXX-domain GET
by
noromanba
2018-05-30 [2018/05/30 07:34:14]
XXX cross-domain GET over CORS abstract
@@ -3,7 +3,6 @@
* @description XXX cross-domain GET over CORS abstract
* @include http://*
* @include https://*
- * @contributor
* @license MIT License https://opensource.org/licenses/MIT
* @javascript_url
*/
@@ -14,31 +13,39 @@
(() => {
'use strict';
- const url = 'https://example.com';
-
- // TODO timeout
- // http://let.hatelabo.jp/noromanba/let/hJmc6caCwdFz
+ // timeout
+ // https://gist.github.com/noromanba/7e76cd75d15e27b102007298a8156d8f
+ const controller = new AbortController();
+ const signal = controller.signal;
+ const TIMEOUT = 5000;
+ const timer = setTimeout(() => controller.abort(), TIMEOUT);
// XXX this server down/glitch so often
- fetch('https://crossorigin.me/' + url, {
+ const PROXY = 'https://crossorigin.me';
+
+ const url = 'https://example.com';
+ fetch(PROXY + url, {
mode: 'cors',
+ signal,
})
.then(res => {
if (!res.ok) {
return Promise.reject(new Error(res.status, res.statusText));
}
-
return res.text();
})
.then(data => {
// do something
- console.log(data);
+ console.dir(data);
+ return data;
})
.catch(err => {
console.warn(err.message, err);
})
.finally(() => {
+ clearTimeout(timer);
console.log('settled');
});
})();
+
/*
* @title XXX-domain GET
* @description XXX cross-domain GET over CORS abstract
* @include http://*
* @include https://*
* @license MIT License https://opensource.org/licenses/MIT
* @javascript_url
*/
// c.f.
// https://crossorigin.me
// https://github.com/technoboy10/crossorigin.me
(() => {
'use strict';
// timeout
// https://gist.github.com/noromanba/7e76cd75d15e27b102007298a8156d8f
const controller = new AbortController();
const signal = controller.signal;
const TIMEOUT = 5000;
const timer = setTimeout(() => controller.abort(), TIMEOUT);
// XXX this server down/glitch so often
const PROXY = 'https://crossorigin.me';
const url = 'https://example.com';
fetch(PROXY + url, {
mode: 'cors',
signal,
})
.then(res => {
if (!res.ok) {
return Promise.reject(new Error(res.status, res.statusText));
}
return res.text();
})
.then(data => {
// do something
console.dir(data);
return data;
})
.catch(err => {
console.warn(err.message, err);
})
.finally(() => {
clearTimeout(timer);
console.log('settled');
});
})();
- Permalink
- このページへの個別リンクです。
- RAW
- 書かれたコードへの直接のリンクです。
- Packed
- 文字列が圧縮された書かれたコードへのリンクです。
- Userscript
- Greasemonkey 等で利用する場合の .user.js へのリンクです。
- Loader
- @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
- Metadata
- コード中にコメントで @xxx と書かれたメタデータの JSON です。