XXX-domain GET

    @@ -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 です。

History

  1. 2018/05/30 07:34:14 - 2018-05-30
  2. 2018/05/19 15:25:35 - 2018-05-19
  3. 2018/05/19 14:57:15 - 2018-05-19
  4. 2018/05/19 14:29:10 - 2018-05-19
  5. 2018/05/19 14:28:10 - 2018-05-19
  6. 2018/05/18 22:14:19 - 2018-05-18
  7. 2018/05/18 22:13:39 - 2018-05-18
  8. 2018/05/18 22:04:11 - 2018-05-18
  9. 2018/05/18 21:54:42 - 2018-05-18