fix(ThemeColor): Make all pages support `theme-color`

This commit is contained in:
haitaoo 2023-05-19 12:50:23 +08:00
parent d6b758993c
commit c63267713c
3 changed files with 140 additions and 38 deletions

View File

@ -3,13 +3,14 @@
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" /> <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
<meta name="theme-color" content="#0033FF" /> <meta name="theme-color" />
<meta name="generator" content="Answer - https://github.com/answerdev/answer"> <meta name="generator" content="Answer - https://github.com/answerdev/answer">
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" /> <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
</head> </head>
<body> <body>
<noscript>You need to enable JavaScript to run this app.</noscript> <noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"> <div id="root">
<div id="spin-mask"> <div id="spin-mask">
<noscript> <noscript>
<style> <style>
@ -17,7 +18,7 @@
display: none !important; display: none !important;
} }
#protect-brower { #protect-browser {
display: none; display: none;
} }
</style> </style>
@ -54,7 +55,7 @@
animation: 0.75s linear infinite _doc-spin; animation: 0.75s linear infinite _doc-spin;
} }
#protect-brower { #protect-browser {
padding: 20px; padding: 20px;
text-align: center; text-align: center;
} }
@ -62,8 +63,9 @@
<div id="spin-container"> <div id="spin-container">
<div class="spinner"></div> <div class="spinner"></div>
</div> </div>
<div id="protect-brower"></div> <div id="protect-browser"></div>
</div> </div>
</div> </div>
</body> </body>
<script> <script>
@ -91,20 +93,20 @@
name: 'IE', name: 'IE',
} }
]; ];
function getBrowerTypeAndVersion(){ function getBrowserTypeAndVersion(){
var brower = { var browser = {
name: '', name: '',
version: '' version: ''
}; };
var ua = navigator.userAgent.toLowerCase(); var ua = navigator.userAgent.toLowerCase();
var s; var s;
((ua.indexOf("compatible") > -1 && ua.indexOf("MSIE") > -1) || (ua.indexOf('Trident') > -1 && ua.indexOf("rv:11.0") > -1)) ? brower = { name: 'IE', version: '' } : ((ua.indexOf("compatible") > -1 && ua.indexOf("MSIE") > -1) || (ua.indexOf('Trident') > -1 && ua.indexOf("rv:11.0") > -1)) ? browser = { name: 'IE', version: '' } :
(s = ua.match(/edge\/([\d\.]+)/)) ? brower = { name: 'Edge', version: s[1] } : (s = ua.match(/edge\/([\d\.]+)/)) ? browser = { name: 'Edge', version: s[1] } :
(s = ua.match(/firefox\/([\d\.]+)/)) ? brower = { name: 'Firefox', version: s[1] } : (s = ua.match(/firefox\/([\d\.]+)/)) ? browser = { name: 'Firefox', version: s[1] } :
(s = ua.match(/chrome\/([\d\.]+)/)) ? brower = { name: 'Chrome', version: s[1] } : (s = ua.match(/chrome\/([\d\.]+)/)) ? browser = { name: 'Chrome', version: s[1] } :
(s = ua.match(/version\/([\d\.]+).*safari/)) ? brower = { name: 'Safari', version: s[1] } : brower = { name: 'unknown', version: '' }; (s = ua.match(/version\/([\d\.]+).*safari/)) ? browser = { name: 'Safari', version: s[1] } : browser = { name: 'unknown', version: '' };
// 根据关系进行判断 // 根据关系进行判断
return brower; return browser;
} }
function compareVersion(version1, version2) { function compareVersion(version1, version2) {
@ -129,25 +131,23 @@
return 0; return 0;
} }
const browerInfo = getBrowerTypeAndVersion(); const browserInfo = getBrowserTypeAndVersion();
if (browserInfo.name === 'IE') {
if (browerInfo.name === 'IE') { const div = document.getElementById('protect-browser');
const div = document.getElementById('protect-brower');
div.innerText = 'Sorry, this site does not support Internet Explorer. In order to avoid affecting the normal use of our features, please use a more modern browser such as Edge, Firefox, Chrome, or Safari.' div.innerText = 'Sorry, this site does not support Internet Explorer. In order to avoid affecting the normal use of our features, please use a more modern browser such as Edge, Firefox, Chrome, or Safari.'
} else { } else {
const notSupport = defaultList.some(item => { const notSupport = defaultList.some(item => {
if (item.name === browerInfo.name) { if (item.name === browserInfo.name) {
return compareVersion(browerInfo.version, item.version) === -1; return compareVersion(browserInfo.version, item.version) === -1;
} }
return false; return false;
}); });
if (notSupport) { if (notSupport) {
const div = document.getElementById('protect-brower'); const div = document.getElementById('protect-browser');
div.innerText = 'The current browser version is too low, in order not to affect the normal use of the function, please upgrade the browser to the latest version.' div.innerText = 'The current browser version is too low, in order not to affect the normal use of the function, please upgrade the browser to the latest version.'
} }
} }
</script> </script>
</html> </html>

View File

@ -1,4 +1,4 @@
import { FC } from 'react'; import { FC, useLayoutEffect } from 'react';
import { Helmet } from 'react-helmet-async'; import { Helmet } from 'react-helmet-async';
import Color from 'color'; import Color from 'color';
@ -11,10 +11,17 @@ const Index: FC = () => {
let primaryColor; let primaryColor;
if (theme_config?.[theme]?.primary_color) { if (theme_config?.[theme]?.primary_color) {
primaryColor = Color(theme_config[theme].primary_color); primaryColor = Color(theme_config[theme].primary_color);
document
.querySelector('meta[name="theme-color"]')
?.setAttribute('content', primaryColor.hex());
} }
const setThemeColor = () => {
const themeMetaNode = document.querySelector('meta[name="theme-color"]');
if (themeMetaNode) {
const themeColor = primaryColor ? primaryColor.hex() : '#0033ff';
themeMetaNode.setAttribute('content', themeColor);
}
};
useLayoutEffect(() => {
setThemeColor();
}, [primaryColor]);
return ( return (
<Helmet> <Helmet>

View File

@ -3,8 +3,9 @@
<html> <html>
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>{{.title}}</title> <title>{{.title}}</title>
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
<meta name="theme-color" />
<meta name="description" content="{{.description}}" data-rh="true" /> <meta name="description" content="{{.description}}" data-rh="true" />
<meta name="generator" content="Answer {{.Version}} - https://github.com/answerdev/answer version {{.Revision}}"> <meta name="generator" content="Answer {{.Version}} - https://github.com/answerdev/answer version {{.Revision}}">
{{if .keywords }}<meta name="keywords" content="{{.keywords}}" data-rh="true" />{{end}} {{if .keywords }}<meta name="keywords" content="{{.keywords}}" data-rh="true" />{{end}}
@ -37,19 +38,23 @@
</head> </head>
<body> <body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"> <div id="root">
<div id="spin-mask" hidden>
<script> <div id="spin-mask">
try { <noscript>
document.querySelector('#spin-mask').removeAttribute('hidden'); <style>
} catch (ex) {} #spin-mask {
</script> display: none !important;
}
#protect-browser {
display: none;
}
</style>
</noscript>
<style> <style>
@keyframes _doc-spin { @keyframes _doc-spin {
to { to { transform: rotate(360deg) }
transform: rotate(360deg);
}
} }
#spin-mask { #spin-mask {
position: fixed; position: fixed;
@ -71,17 +76,23 @@
display: inline-block; display: inline-block;
width: 2rem; width: 2rem;
height: 2rem; height: 2rem;
vertical-align: -0.125em; vertical-align: -.125em;
border: 0.25rem solid currentColor; border: .25rem solid currentColor;
border-right-color: transparent; border-right-color: transparent;
color: rgba(108, 117, 125, 0.75); color: rgba(108, 117, 125, .75);
border-radius: 50%; border-radius: 50%;
animation: 0.75s linear infinite _doc-spin; animation: 0.75s linear infinite _doc-spin;
} }
#protect-browser {
padding: 20px;
text-align: center;
}
</style> </style>
<div id="spin-container"> <div id="spin-container">
<div class="spinner"></div> <div class="spinner"></div>
</div> </div>
<div id="protect-browser"></div>
</div> </div>
<nav id="header" class="sticky-top navbar navbar-expand-lg navbar-dark"> <nav id="header" class="sticky-top navbar navbar-expand-lg navbar-dark">
@ -119,6 +130,90 @@
</div> </div>
</nav> </nav>
<div class="position-relative page-wrap">{{end}}</div> <div class="position-relative page-wrap">{{end}}</div>
</div> </div>
</body> </body>
<script>
/**
* @description: Prompt that the browser version is too low
*/
const defaultList = [
{
name: 'Edge',
version: '100'
},
{
name: 'Firefox',
version: '100'
},
{
name: 'Chrome',
version: '90'
},
{
name: 'Safari',
version: '15'
},
{
name: 'IE',
}
];
function getBrowserTypeAndVersion(){
var browser = {
name: '',
version: ''
};
var ua = navigator.userAgent.toLowerCase();
var s;
((ua.indexOf("compatible") > -1 && ua.indexOf("MSIE") > -1) || (ua.indexOf('Trident') > -1 && ua.indexOf("rv:11.0") > -1)) ? browser = { name: 'IE', version: '' } :
(s = ua.match(/edge\/([\d\.]+)/)) ? browser = { name: 'Edge', version: s[1] } :
(s = ua.match(/firefox\/([\d\.]+)/)) ? browser = { name: 'Firefox', version: s[1] } :
(s = ua.match(/chrome\/([\d\.]+)/)) ? browser = { name: 'Chrome', version: s[1] } :
(s = ua.match(/version\/([\d\.]+).*safari/)) ? browser = { name: 'Safari', version: s[1] } : browser = { name: 'unknown', version: '' };
// 根据关系进行判断
return browser;
}
function compareVersion(version1, version2) {
var v1 = version1.split('.');
var v2 = version2.split('.');
var len = Math.max(v1.length, v2.length);
while (v1.length < len) {
v1.push('0');
}
while (v2.length < len) {
v2.push('0');
}
for (var i = 0; i < len; i++) {
var num1 = parseInt(v1[i]);
var num2 = parseInt(v2[i]);
if (num1 >= num2) {
return 1;
} else if (num1 < num2) {
return -1;
}
}
return 0;
}
const browserInfo = getBrowserTypeAndVersion();
if (browserInfo.name === 'IE') {
const div = document.getElementById('protect-browser');
div.innerText = 'Sorry, this site does not support Internet Explorer. In order to avoid affecting the normal use of our features, please use a more modern browser such as Edge, Firefox, Chrome, or Safari.'
} else {
const notSupport = defaultList.some(item => {
if (item.name === browserInfo.name) {
return compareVersion(browserInfo.version, item.version) === -1;
}
return false;
});
if (notSupport) {
const div = document.getElementById('protect-browser');
div.innerText = 'The current browser version is too low, in order not to affect the normal use of the function, please upgrade the browser to the latest version.'
}
}
</script>
</html> </html>