Skip to content

Instantly share code, notes, and snippets.

@wangye
Created March 5, 2022 05:25
Show Gist options
  • Save wangye/116bf5a1da1d3f173e2e9cee51bd96f9 to your computer and use it in GitHub Desktop.
Save wangye/116bf5a1da1d3f173e2e9cee51bd96f9 to your computer and use it in GitHub Desktop.
//
// JavaScript Polyfills ( Simplified Chinese )
// https://wangye.org/posts/2022/03/polyfills-in-javascript-for-newer-features.html
//
var polyfill_features = [
"MediaQueryList.prototype.addEventListener",
"String.prototype.startsWith",
"String.prototype.trim",
"Element.prototype.classList",
"document.querySelector",
"pageYOffset",
"Array.prototype.forEach",
"matchMedia",
"localStorage",
"URLSearchParams",
"URL",
"JSON",
"Object.entries"
];
var cache_version = "v1";
// minimum browser version requirements
var min_browsers = {
"Firefox": 80,
"Edg": 80,
"Chrome": 80,
"Safari": 15
};
; (function(window, document, js_features, jsf_version, browsers) {
var cacheKey = "js-features";
var JsPolyfill = function(js_features, jsf_version) {
function getPreDetectedResult() {
var jsf_a_unsupports = js_features.join("%2C");
if (window.localStorage) {
var jsf = window.localStorage.getItem(cacheKey);
if (jsf !== null && jsf.indexOf("^") > 0) {
var jsf_a = jsf.split("^");
var jsf_a_version = jsf_a[0];
if (jsf_a_version === jsf_version) {
if (jsf_a.length > 1) {
jsf_a_unsupports = jsf_a[1];
} else {
jsf_a_unsupports = "";
}
}
}
}
return jsf_a_unsupports.split("%2C");
}
function getUnsupportedJsFeatures() {
var unsupports = [];
var jsf_a_features = getPreDetectedResult();
var jsf_a_prev_features = "#" + js_features.join("#") + "#";
for (var i = 0; i < jsf_a_features.length; i++) {
if (jsf_a_prev_features.indexOf("#" + jsf_a_features[i] + "#") === -1) {
console.warn("Pollyfill: Unknown or suspicious code detected," +
" skipped and continue.");
continue;
}
if ((function() {
if (jsf_a_features[i].indexOf("Element.prototype.") === 0) {
return (typeof Element === 'undefined' ||
!(jsf_a_features[i].substr("Element.prototype.".length)
in Element.prototype));
} else {
return eval("typeof " + jsf_a_features[i]) === 'undefined';
}
})()) {
unsupports.push(jsf_a_features[i]);
}
}
return (unsupports.length > 0) ? unsupports.join("%2C") : "";
}
var params = getUnsupportedJsFeatures();
if (params !== "") {
window.localStorage &&
window.localStorage.setItem(cacheKey, jsf_version + "^" + params);
var url = "https://polyfill.io/v3/polyfill.min.js?features=" + params;
document.write('<scr' + 'ipt type="text/javascript" src="' + url
+ '" crossorigin="anonymous"><\/scri' + 'pt>');
} else {
window.localStorage &&
window.localStorage.setItem(cacheKey, jsf_version + "^");
}
};
function isBrowserCompatibleWith() {
if (window.localStorage &&
window.localStorage.getItem(cacheKey) === (jsf_version + "^")) {
return true;
}
if (typeof browsers === 'object' &&
window.navigator && window.navigator.userAgent) {
return (function() {
var userAgent = window.navigator.userAgent;
for (key in browsers) {
if (userAgent.indexOf(key + '/') !== -1) {
var version = userAgent.split(key + '/')[1];
if (key == 'Safari') {
version = userAgent.split('Version/')[1];
}
version_a = version.split('.');
version_s = parseInt(version_a[0], 10);
if (!isNaN(version_s) && version_s >= browsers[key]) {
window.localStorage.setItem(cacheKey, jsf_version + "^");
return true;
}
break;
}
}
return false;
})();
} else {
return false;
}
}
if (!isBrowserCompatibleWith())
JsPolyfill(js_features, jsf_version);
})(window, document,
polyfill_features, cache_version, min_browsers);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment