You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

browser-compatibility.js 6.4 kB

2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. (()=>{
  2. function runCodeWithFunction(obj) {
  3. return Function(`"use strict"; return (${obj})`)();
  4. }
  5. function supportsPseudoClass(clazz) {
  6. const style = document.createElement('style');
  7. style.innerHTML = clazz + '{}';
  8. document.head.appendChild(style); // required, or style.sheet === null
  9. const result = style.sheet.cssRules.length === 1;
  10. style.remove(); // document.head.removeChild(style);
  11. return result;
  12. }
  13. const features = [
  14. {name: "Optional chaining (?.) / 可选链操作符(?.)", version:{firefox:74,chrome:80,safari:13.4}, url: "https://caniuse.com/mdn-javascript_operators_optional_chaining", test: ()=>Boolean(runCodeWithFunction("undefined?.undefined || true"))},
  15. {name: "Nullish coalescing operator (??) / 空值合并操作符(??)", version:{firefox:72,chrome:80,safari:13.4}, url: "https://caniuse.com/mdn-javascript_operators_nullish_coalescing", test: ()=>Boolean(runCodeWithFunction("undefined ?? true"))},
  16. {name: "BigInt value (1n) / BigInt 数据类型(1n)", version:{firefox:68,chrome:67,safari:14}, url: "https://caniuse.com/bigint", test: ()=>Boolean(runCodeWithFunction("1n"))},
  17. {name: "CSS selector: :where() / CSS选择器: :where()", version:{firefox:78,chrome:88,safari:14}, url: "https://caniuse.com/mdn-css_selectors_where", test: ()=>supportsPseudoClass(":where()")},
  18. {name: "CSS selector: :not() / CSS选择器: :not()", version:{firefox:84,chrome:88,safari:9}, url: "https://caniuse.com/css-not-sel-list", test: ()=>supportsPseudoClass(":not(html)")},
  19. //{name: "CSS selector: :has() / CSS选择器: :has()", version:{firefox:121,chrome:105,safari:15.4}, url: "https://caniuse.com/css-has", test: ()=>supportsPseudoClass(":has(html)")},
  20. {name: "Private class fields (#name) / 类私有域(#name)", version:{firefox:90,chrome:74,safari:14.5}, url: "https://caniuse.com/mdn-javascript_classes_private_class_fields", test: ()=>Boolean(runCodeWithFunction("class test {#v = 0;}, true"))},
  21. {name: "Dialog element / Dialog 元素", version:{firefox:98,chrome:37,safari:15.4}, url: "https://caniuse.com/dialog", test: ()=>Boolean(window.HTMLDialogElement)},
  22. //{name: "Class static initialization blocks / 静态初始化块", version:{firefox:93,chrome:94,safari:16.4}, url: "https://caniuse.com/mdn-javascript_classes_static_initialization_blocks", test: ()=>Boolean(runCodeWithFunction("class test { static { this.staticProperty = true;};}, true"))},
  23. {name: "Array.prototype.toSorted()", version:{firefox:115,chrome:110,safari:16.0}, url: "https://caniuse.com/mdn-javascript_builtins_array_tosorted", test: ()=>Boolean(Array.prototype.toSorted)},
  24. {name: "Set.prototype.isDisjointFrom()", version:{firefox:127,chrome:122,safari:17.0}, url: "https://caniuse.com/mdn-javascript_builtins_set_isdisjointfrom", test: ()=>Boolean(Set.prototype.isDisjointFrom)},
  25. //{name: "Duplicate named capture group / 重复的正则表达式匹配命名组", version:{firefox:129,chrome:125,safari:17.0}, url: "https://caniuse.com/mdn-javascript_regular_expressions_named_capturing_group_duplicate_named_capturing_groups", test: ()=>Boolean(runCodeWithFunction("/(?<year>\\d{4})-\\d{2}|\\d{2}-(?<year>\\d{4})/"))},
  26. ];
  27. const unsupportFeatures = features.filter(feature=>{
  28. try {
  29. return !feature.test();
  30. } catch (e) {
  31. if (e.name !== 'SyntaxError')
  32. console.error(e);
  33. return true;
  34. }
  35. });
  36. if (unsupportFeatures.length) {
  37. const browserVersion = ((UA)=>{
  38. let regRes;
  39. if (regRes = /\b(Firefox|Chrome)\/([\d\.]+)/ig.exec(UA)) {
  40. return `${regRes[1]} ${regRes[2]}`;
  41. } else if (regRes = /\bVersion\/([\d\.]+)\s+.*\b(Safari)\//ig.exec(UA)) {
  42. return `${regRes[2]} ${regRes[1]}`;
  43. } else {
  44. UA;
  45. }
  46. })(navigator.userAgent);
  47. //支持的最低版本
  48. const needBrowserVersion = unsupportFeatures.reduce((pre,{version})=>{
  49. pre.firefox = Math.max(pre.firefox,version.firefox);
  50. pre.chrome = Math.max(pre.chrome,version.chrome);
  51. pre.safari = Math.max(pre.safari,version.safari);
  52. return pre;
  53. }, {firefox:0,chrome:0,safari:0});
  54. let alertStr;
  55. if (/^zh-(?:han(?:s|t)-)?/.test(navigator.language)) {
  56. alertStr =
  57. `<p lang="zh">🙁浏览器内核版本太老<br>
  58. 您的浏览器版本为: ${browserVersion}<br>
  59. 您的浏览器内核不支持本程序使用的以下技术
  60. <ol>
  61. ${unsupportFeatures.map(feature=>`<li><a href="${feature.url}">${feature.name}</a></li>`).join('')}
  62. </ol>
  63. 请更新您的浏览器内核到 Firefox(火狐) ≥ ${needBrowserVersion.firefox} 或 Chrome(谷歌) ≥ ${needBrowserVersion.chrome} 或 Safari ≥ ${needBrowserVersion.safari}。</p>`;
  64. } else {
  65. alertStr =
  66. `<p lang="en">🙁Browser kernel is too old<br>
  67. Your browser is: ${browserVersion}<br>
  68. Your browser kernel does not support the following technologies used by this program:
  69. <ol>
  70. ${unsupportFeatures.map(feature=>`<li><a href="${feature.url}">${feature.name}</a></li>`).join('')}
  71. </ol>
  72. Please update your browser core to Firefox ≥ ${needBrowserVersion.firefox} or Chrome ≥ ${needBrowserVersion.chrome} or Safari ≥ ${needBrowserVersion.safari}</p>`;
  73. }
  74. //alert(alertStr);
  75. const event = window.addEventListener("load", ()=>{
  76. document.body.insertAdjacentHTML("afterbegin", alertStr);
  77. window.removeEventListener("load", event);
  78. });
  79. }
  80. if (/\b(?:MicroMessenger|WeChat|Weixin|QQ|AliApp)\b/.test(navigator.userAgent)) {
  81. const mask = document.createElement("div");
  82. mask.id = "denied-mask";
  83. const css = `
  84. #denied-mask {
  85. position: fixed;
  86. height: 100%;
  87. width: 100%;
  88. top: 0;
  89. left: 0;
  90. background-color: #000A;
  91. }
  92. .alert {
  93. font-size: 2em;
  94. font-weight: bold;
  95. color: white;
  96. text-align: center;
  97. }
  98. `;
  99. const style = mask.appendChild(document.createElement("style"));
  100. style.appendChild(document.createTextNode(css));
  101. const alertDiv = mask.appendChild(document.createElement("div"));
  102. alertDiv.className = "alert";
  103. alertDiv.innerHTML = `请勿使用APP内置浏览器,会有功能缺失<br>点击菜单使用正常浏览器打开↗`;
  104. const removeMe = mask.appendChild(document.createElement("button"));
  105. removeMe.append("我知道了");
  106. removeMe.onclick = ()=>{
  107. mask.remove();
  108. delete mask;
  109. };
  110. const event = window.addEventListener("load", ()=>{
  111. document.body.appendChild(mask);
  112. window.removeEventListener("load", event);
  113. });
  114. }
  115. })();