Greasy Fork

polyfill

Firefox、Opera、Google Chrome向けのpolyfillです。

目前为 2016-03-15 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.greasyfork.cloud/scripts/17895/113303/polyfill.js

// ==UserScript==
// @name        polyfill
// @description Firefox、Opera、Google Chrome向けのpolyfillです。
// @version     1.1.0
// @license     Mozilla Public License Version 2.0 (MPL 2.0); https://www.mozilla.org/MPL/2.0/
// @compatible  Firefox
// @compatible  Opera 36以降
// @compatible  Chrome
// @author      100の人
// @homepage    https://greasyfork.org/scripts/17895
// ==/UserScript==

(function () {
'use strict';

// Polyfill for Firefox, Opera, and Google Chrome
if (new URLSearchParams('?').has('?')) {
	URLSearchParams = new Proxy(URLSearchParams, {
		construct: function (URLSearchParams, argumentsList) {
			if (argumentsList.length > 0 && !(argumentsList[0] instanceof URLSearchParams)) {
				argumentsList[0] = String(argumentsList[0]).replace(/^\?/, '');
			}
			return new URLSearchParams(...argumentsList);
		},
	});
}

// Polyfill for Firefox 38 ESR
if (!''.includes) {
	/** @see [Bug 1102219 – Rename String.prototype.contains to String.prototype.includes]{https://bugzilla.mozilla.org/show_bug.cgi?id=1102219} */
	Object.defineProperty(String.prototype, 'includes', {
		writable: true,
		enumerable: false,
		configurable: true,
		value: String.prototype.contains,
	});
}
	
// Polyfill for Opera and Google Chrome
if (!(Symbol.iterator in NodeList.prototype)) {
	Object.defineProperties(NodeList.prototype, /** @lends NodeList# */ {
		/**
		 * @see [Issue 401699 - chromium - Add iterator support to NodeList and friends]{@link https://code.google.com/p/chromium/issues/detail?id=401699}
		 * @returns {Iterator.<Array.<number, Node>>}
		 * @name NodeList#@@iterator
		 */
		[Symbol.iterator]: {
			writable: true,
			enumerable: false,
			configurable: true,
			value: function* () {
				for (var i = 0, l = this.length; i < l; i++) {
					yield this[i];
				}
			}
		},
		/**
		 * @param {Function} callback
		 * @param {*} thisArg
		 * @function
		 */
		forEach: {
			writable: true,
			enumerable: true,
			configurable: true,
			value: Array.prototype.forEach
		},
		/**
		 * @returns {Iterator.<Array.<number, Node>>}
		 * @function
		 */
		entries: {
			writable: true,
			enumerable: true,
			configurable: true,
			value: function* () {
				for (var i = 0, l = this.length; i < l; i++) {
					yield [i, this[i]];
				}
			}
		},
		/**
		 * @returns {Iterator.<number>}
		 * @function
		 */
		keys: {
			writable: true,
			enumerable: true,
			configurable: true,
			value: function* () {
				for (var i = 0, l = this.length; i < l; i++) {
					yield i;
				}
			}
		},
		/**
		 * @returns {Iterator.<Node>}
		 * @function
		 */
		values: {
			writable: true,
			enumerable: true,
			configurable: true,
			value: function* () {
				for (var i = 0, l = this.length; i < l; i++) {
					yield this[i];
				}
			}
		},
	});
}

})();