feat: 升级版本至1.4.0并添加外部资源备用机制
- 版本号从1.3.5升级至1.4.0 - 创建外部资源备用加载系统,解决国内访问问题 - 添加Google Fonts本地备用字体文件 - 集成Geetest验证码本地备用版本 - 实现QRCode.js本地备用功能 - 创建智能资源加载器,自动切换到本地资源 - 修改所有外部资源引用,支持自动备用机制 - 添加资源配置文件,便于管理和维护
This commit is contained in:
127
assets/vendor/external/resource-loader.js
vendored
Normal file
127
assets/vendor/external/resource-loader.js
vendored
Normal file
@@ -0,0 +1,127 @@
|
||||
/* 外部资源加载器 - 自动备用机制 */
|
||||
(function() {
|
||||
'use strict';
|
||||
|
||||
window.ArgonResourceLoader = {
|
||||
// 资源映射表
|
||||
resourceMap: {
|
||||
'fonts.googleapis.com/css?family=Open+Sans': '/wp-content/themes/argon/assets/vendor/external/fonts/open-sans.css',
|
||||
'fonts.googleapis.com/css?family=Noto+Serif+SC': '/wp-content/themes/argon/assets/vendor/external/fonts/noto-serif-sc.css',
|
||||
'static.geetest.com/v4/gt4.js': '/wp-content/themes/argon/assets/vendor/external/geetest/gt4.js',
|
||||
'cdn.jsdelivr.net/npm/qrcodejs@1.0.0/qrcode.min.js': '/wp-content/themes/argon/assets/vendor/external/qrcode/qrcode.min.js'
|
||||
},
|
||||
|
||||
// 加载CSS资源
|
||||
loadCSS: function(url, fallbackUrl) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
var link = document.createElement('link');
|
||||
link.rel = 'stylesheet';
|
||||
link.href = url;
|
||||
|
||||
var timeout = setTimeout(function() {
|
||||
console.warn('CSS资源加载超时,使用本地备用:', url);
|
||||
if (fallbackUrl) {
|
||||
ArgonResourceLoader.loadCSS(fallbackUrl).then(resolve).catch(reject);
|
||||
} else {
|
||||
reject(new Error('CSS加载失败且无备用资源'));
|
||||
}
|
||||
}, 5000);
|
||||
|
||||
link.onload = function() {
|
||||
clearTimeout(timeout);
|
||||
resolve();
|
||||
};
|
||||
|
||||
link.onerror = function() {
|
||||
clearTimeout(timeout);
|
||||
console.warn('CSS资源加载失败,使用本地备用:', url);
|
||||
if (fallbackUrl) {
|
||||
ArgonResourceLoader.loadCSS(fallbackUrl).then(resolve).catch(reject);
|
||||
} else {
|
||||
reject(new Error('CSS加载失败且无备用资源'));
|
||||
}
|
||||
};
|
||||
|
||||
document.head.appendChild(link);
|
||||
});
|
||||
},
|
||||
|
||||
// 加载JS资源
|
||||
loadJS: function(url, fallbackUrl) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
var script = document.createElement('script');
|
||||
script.src = url;
|
||||
script.async = true;
|
||||
|
||||
var timeout = setTimeout(function() {
|
||||
console.warn('JS资源加载超时,使用本地备用:', url);
|
||||
if (fallbackUrl) {
|
||||
ArgonResourceLoader.loadJS(fallbackUrl).then(resolve).catch(reject);
|
||||
} else {
|
||||
reject(new Error('JS加载失败且无备用资源'));
|
||||
}
|
||||
}, 5000);
|
||||
|
||||
script.onload = function() {
|
||||
clearTimeout(timeout);
|
||||
resolve();
|
||||
};
|
||||
|
||||
script.onerror = function() {
|
||||
clearTimeout(timeout);
|
||||
console.warn('JS资源加载失败,使用本地备用:', url);
|
||||
if (fallbackUrl) {
|
||||
ArgonResourceLoader.loadJS(fallbackUrl).then(resolve).catch(reject);
|
||||
} else {
|
||||
reject(new Error('JS加载失败且无备用资源'));
|
||||
}
|
||||
};
|
||||
|
||||
document.head.appendChild(script);
|
||||
});
|
||||
},
|
||||
|
||||
// 智能加载资源
|
||||
smartLoad: function(url, type) {
|
||||
var fallbackUrl = null;
|
||||
|
||||
// 查找备用资源
|
||||
for (var key in this.resourceMap) {
|
||||
if (url.indexOf(key) !== -1) {
|
||||
fallbackUrl = this.resourceMap[key];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (type === 'css') {
|
||||
return this.loadCSS(url, fallbackUrl);
|
||||
} else if (type === 'js') {
|
||||
return this.loadJS(url, fallbackUrl);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 自动替换现有的资源加载
|
||||
var originalCreateElement = document.createElement;
|
||||
document.createElement = function(tagName) {
|
||||
var element = originalCreateElement.call(document, tagName);
|
||||
|
||||
if (tagName.toLowerCase() === 'script') {
|
||||
var originalSetAttribute = element.setAttribute;
|
||||
element.setAttribute = function(name, value) {
|
||||
if (name === 'src' && value) {
|
||||
// 检查是否需要备用资源
|
||||
for (var key in ArgonResourceLoader.resourceMap) {
|
||||
if (value.indexOf(key) !== -1) {
|
||||
console.log('检测到外部JS资源,准备备用方案:', value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return originalSetAttribute.call(this, name, value);
|
||||
};
|
||||
}
|
||||
|
||||
return element;
|
||||
};
|
||||
})();
|
||||
Reference in New Issue
Block a user