您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
https://github.com/jonnyreeves/fetch-readablestream/blob/master/README.md
当前为
此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/29306/191128/fetch-readablestream.js
- (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.fetchStream = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
- 'use strict';
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = defaultTransportFactory;
- var _fetch = require('./fetch');
- var _fetch2 = _interopRequireDefault(_fetch);
- var _xhr = require('./xhr');
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
- // selected is used to cache the detected transport.
- var selected = null;
- // defaultTransportFactory selects the most appropriate transport based on the
- // capabilities of the current environment.
- function defaultTransportFactory() {
- if (!selected) {
- selected = detectTransport();
- }
- return selected;
- }
- function detectTransport() {
- if (typeof Response !== 'undefined' && Response.prototype.hasOwnProperty("body")) {
- // fetch with ReadableStream support.
- return _fetch2.default;
- }
- var mozChunked = 'moz-chunked-arraybuffer';
- if (supportsXhrResponseType(mozChunked)) {
- // Firefox, ArrayBuffer support.
- return (0, _xhr.makeXhrTransport)({
- responseType: mozChunked,
- responseParserFactory: function responseParserFactory() {
- return function (response) {
- return new Uint8Array(response);
- };
- }
- });
- }
- // Bog-standard, expensive, text concatenation with byte encoding :(
- return (0, _xhr.makeXhrTransport)({
- responseType: 'text',
- responseParserFactory: function responseParserFactory() {
- var encoder = new TextEncoder();
- var offset = 0;
- return function (response) {
- var chunk = response.substr(offset);
- offset = response.length;
- return encoder.encode(chunk, { stream: true });
- };
- }
- });
- }
- function supportsXhrResponseType(type) {
- try {
- var tmpXhr = new XMLHttpRequest();
- tmpXhr.responseType = type;
- return tmpXhr.responseType === type;
- } catch (e) {/* IE throws on setting responseType to an unsupported value */}
- return false;
- }
- },{"./fetch":3,"./xhr":6}],2:[function(require,module,exports){
- module.exports = require("./index").default;
- },{"./index":4}],3:[function(require,module,exports){
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = fetchRequest;
- // thin wrapper around `fetch()` to ensure we only expose the properties provided by
- // the XHR polyfil; / fetch-readablestream Response API.
- function fetchRequest(url, options) {
- return fetch(url, options).then(function (r) {
- return {
- body: r.body,
- headers: r.headers,
- ok: r.ok,
- status: r.status,
- statusText: r.statusText,
- url: r.url
- };
- });
- }
- },{}],4:[function(require,module,exports){
- 'use strict';
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = fetchStream;
- var _defaultTransportFactory = require('./defaultTransportFactory');
- var _defaultTransportFactory2 = _interopRequireDefault(_defaultTransportFactory);
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
- function fetchStream(url) {
- var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
- var transport = options.transport;
- if (!transport) {
- transport = fetchStream.transportFactory();
- }
- return transport(url, options);
- }
- // override this function to delegate to an alternative transport function selection
- // strategy; useful when testing.
- fetchStream.transportFactory = _defaultTransportFactory2.default;
- },{"./defaultTransportFactory":1}],5:[function(require,module,exports){
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- // Headers is a partial polyfill for the HTML5 Headers class.
- var Headers = exports.Headers = function () {
- function Headers() {
- var _this = this;
- var h = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
- _classCallCheck(this, Headers);
- this.h = {};
- if (h instanceof Headers) {
- h.forEach(function (value, key) {
- return _this.append(key, value);
- });
- }
- Object.getOwnPropertyNames(h).forEach(function (key) {
- return _this.append(key, h[key]);
- });
- }
- _createClass(Headers, [{
- key: "append",
- value: function append(key, value) {
- key = key.toLowerCase();
- if (!Array.isArray(this.h[key])) {
- this.h[key] = [];
- }
- this.h[key].push(value);
- }
- }, {
- key: "set",
- value: function set(key, value) {
- this.h[key.toLowerCase()] = [value];
- }
- }, {
- key: "has",
- value: function has(key) {
- return Array.isArray(this.h[key.toLowerCase()]);
- }
- }, {
- key: "get",
- value: function get(key) {
- key = key.toLowerCase();
- if (Array.isArray(this.h[key])) {
- return this.h[key][0];
- }
- }
- }, {
- key: "getAll",
- value: function getAll(key) {
- return this.h[key.toLowerCase()].concat();
- }
- }, {
- key: "entries",
- value: function entries() {
- var items = [];
- this.forEach(function (value, key) {
- items.push([key, value]);
- });
- return makeIterator(items);
- }
- // forEach is not part of the official spec.
- }, {
- key: "forEach",
- value: function forEach(callback, thisArg) {
- var _this2 = this;
- Object.getOwnPropertyNames(this.h).forEach(function (key) {
- _this2.h[key].forEach(function (value) {
- return callback.call(thisArg, value, key, _this2);
- });
- }, this);
- }
- }]);
- return Headers;
- }();
- function makeIterator(items) {
- return _defineProperty({
- next: function next() {
- var value = items.shift();
- return {
- done: value === undefined,
- value: value
- };
- }
- }, Symbol.iterator, function () {
- return this;
- });
- }
- },{}],6:[function(require,module,exports){
- 'use strict';
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.makeXhrTransport = makeXhrTransport;
- exports.parseResposneHeaders = parseResposneHeaders;
- var _Headers = require('./polyfill/Headers');
- function makeXhrTransport(_ref) {
- var responseType = _ref.responseType;
- var responseParserFactory = _ref.responseParserFactory;
- return function xhrTransport(url, options) {
- var xhr = new XMLHttpRequest();
- var responseParser = responseParserFactory();
- var responseStreamController = void 0;
- var cancelled = false;
- var responseStream = new ReadableStream({
- start: function start(c) {
- responseStreamController = c;
- },
- cancel: function cancel() {
- cancelled = true;
- xhr.abort();
- }
- });
- var _options$method = options.method;
- var method = _options$method === undefined ? 'GET' : _options$method;
- xhr.open(method, url);
- xhr.responseType = responseType;
- xhr.withCredentials = options.credentials !== 'omit';
- if (options.headers) {
- var _iteratorNormalCompletion = true;
- var _didIteratorError = false;
- var _iteratorError = undefined;
- try {
- for (var _iterator = options.headers.entries()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
- var pair = _step.value;
- xhr.setRequestHeader(pair[0], pair[1]);
- }
- } catch (err) {
- _didIteratorError = true;
- _iteratorError = err;
- } finally {
- try {
- if (!_iteratorNormalCompletion && _iterator.return) {
- _iterator.return();
- }
- } finally {
- if (_didIteratorError) {
- throw _iteratorError;
- }
- }
- }
- }
- return new Promise(function (resolve, reject) {
- if (options.body && (method === 'GET' || method === 'HEAD')) {
- reject(new TypeError("Failed to execute 'fetchStream' on 'Window': Request with GET/HEAD method cannot have body"));
- }
- xhr.onreadystatechange = function () {
- if (xhr.readyState === xhr.HEADERS_RECEIVED) {
- return resolve({
- body: responseStream,
- headers: parseResposneHeaders(xhr.getAllResponseHeaders()),
- ok: xhr.status >= 200 && xhr.status < 300,
- status: xhr.status,
- statusText: xhr.statusText,
- url: makeResponseUrl(xhr.responseURL, url)
- });
- }
- };
- xhr.onerror = function () {
- return reject(new TypeError('Network request failed'));
- };
- xhr.ontimeout = function () {
- reject(new TypeError('Network request failed'));
- };
- xhr.onprogress = function () {
- if (!cancelled) {
- var bytes = responseParser(xhr.response);
- responseStreamController.enqueue(bytes);
- }
- };
- xhr.onload = function () {
- responseStreamController.close();
- };
- xhr.send(options.body);
- });
- };
- }
- function makeHeaders() {
- // Prefer the native method if provided by the browser.
- if (typeof Headers !== 'undefined') {
- return new Headers();
- }
- return new _Headers.Headers();
- }
- function makeResponseUrl(responseUrl, requestUrl) {
- if (!responseUrl) {
- // best guess; note this will not correctly handle redirects.
- if (requestUrl.substring(0, 4) !== "http") {
- return location.origin + requestUrl;
- }
- return requestUrl;
- }
- return responseUrl;
- }
- function parseResposneHeaders(str) {
- var hdrs = makeHeaders();
- if (str) {
- var pairs = str.split('\r\n');
- for (var i = 0; i < pairs.length; i++) {
- var p = pairs[i];
- var index = p.indexOf(': ');
- if (index > 0) {
- var key = p.substring(0, index);
- var value = p.substring(index + 2);
- hdrs.append(key, value);
- }
- }
- }
- return hdrs;
- }
- },{"./polyfill/Headers":5}]},{},[2])(2)
- });