index.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * FeHelper动态工具管理器,主要解决新版本向下兼容,为用户按需找回老版本的功能
  3. * @author zhaoxianlie
  4. */
  5. let DynamicTool = (() => {
  6. // 工具渲染
  7. let render = (toolName, Awesome) => {
  8. Awesome.getToolTpl(toolName).then(html => {
  9. if (!html) {
  10. if (confirm('检测到当前指定的工具还未安装,这就去设置页面安装工具!')) {
  11. location.replace('../options/index.html');
  12. } else {
  13. window.close();
  14. }
  15. return;
  16. }
  17. // 生成界面
  18. document.write(html);
  19. // 页面滤镜:关掉
  20. DarkModeMgr.turnLightAuto();
  21. // 更新静态文件
  22. let list = document.querySelectorAll('dynamic[data-source]');
  23. if (!list.length) return;
  24. let allJs = [];
  25. let allCss = [];
  26. for (let i = 0; i < list.length; i++) {
  27. let elm = list[i];
  28. let fileType = elm.getAttribute('data-type');
  29. let sources = elm.getAttribute('data-source') || '';
  30. let files = sources.split(',').map(source => {
  31. // 去query处理,获得干净的local key
  32. if (source.indexOf('?') !== -1) {
  33. let x = source.split('?');
  34. x.pop();
  35. source = x.join('');
  36. }
  37. return source;
  38. });
  39. if (fileType === 'js') {
  40. allJs = allJs.concat(files);
  41. } else {
  42. allCss = allCss.concat(files);
  43. }
  44. }
  45. Promise.all([Awesome.StorageMgr.get(allCss), Awesome.StorageMgr.get(allJs)]).then(values => {
  46. document.body.style.display = 'block';
  47. allCss = allCss.map(f => values[0][f]).join(' ');
  48. if (allCss.length) {
  49. let node = document.createElement('style');
  50. node.textContent = allCss;
  51. document.head.appendChild(node);
  52. }
  53. allJs = allJs.map(f => values[1][f]).join(';');
  54. allJs.length && window.evalCore.getEvalInstance(window)(allJs);
  55. });
  56. });
  57. };
  58. // 页面初始化
  59. let init = () => {
  60. // 从Query中寻找需要动态渲染的工具名称
  61. let toolName = new URL(location.href).searchParams.get('tool');
  62. if (toolName) {
  63. import('../background/awesome.js').then(dynamicModule => {
  64. render(toolName, dynamicModule.default);
  65. });
  66. } else {
  67. location.replace('../options/index.html');
  68. }
  69. };
  70. return {init}
  71. })();
  72. DynamicTool.init();