javascript - 如何消除 chrome 扩展/Firefox 插件弹出的延迟?
问题描述
我正在从 chrome 和 firefox 构建一个批量下载扩展来下载大量文件。扩展的弹出窗口具有取消/暂停/恢复功能。下载少量文件时,弹出窗口工作正常。但是,当下载大量文件时,弹出菜单需要很长时间才能真正弹出(或者有时根本不会弹出),这可能是因为扩展程序变得非常繁忙并且一次处理大量文件。有没有办法消除这种延迟?
清单.json
"default_popup": "html/popup.html"
},
popup.html
let bgPage = chrome.extension.getBackgroundPage(); //Getting the variables from the background page
let idsOfDownload = [];
idsOfDownload = bgPage.downloadIds;
console.log(idsOfDownload);
let cancel = document.createElement("button");
cancel.id = "cancel";
cancel.className = "btn";
cancel.innerHTML='<i class="fa fa-stop-circle"></i> Cancel All</button>';
document.body.appendChild(cancel);
$('body').width(350);
setInterval(function(){
let downloadString = LZString.decompress(localStorage.getItem('downloadLinks'));
if(downloadString === "") return;
let downloadLinks = JSON.parse(downloadString);
if (downloadLinks !== undefined && downloadLinks !== null && downloadLinks !== "null") {
let status = `Total pending download: ${downloadLinks.length}`;
jQuery("#download-status").html(status);
}
},1000);
$(cancel).click(function () {
chrome.runtime.sendMessage({message: "cancel-download"});
});
解决方案
通常 DOM 很慢,所以如果你有很多元素,你应该只显示可见的东西,并在滚动时按需绘制其余的东西。为此有许多库和框架。
LZString 对于大字符串可能会非常慢,因此请考虑不使用它或在web worker中使用它并通过标准 DOM 消息传递数据。
window.localStorage 是同步的,所以如果数据很大,它也可能很慢,考虑切换到异步存储,如 chrome.storage.local 或 IndexedDB。
最重要的是,使用devtools profiler而不是猜测。分析结果后,您将看到需要修复/重写的内容。弹出窗口有自己的开发工具(请参阅如何打开它)。
推荐阅读
- c++ - 如何修复 OpenCL 冻结?
- node.js - 重定向非 www 和 http - NodeJS 和 Express
- vue.js - Math.round() 作为 v-model 返回值
- python - 如何使用 re.match 查找 URL 的第一部分?
- python - 使用 LpVariable.dicts 设置不同的变量类型和范围
- typescript - .Subscribe() 函数接收值但无法进入函数块
- sql - 为什么子查询字段不会过滤 tablix 但主查询字段会过滤?
- jsf - 我的表格仅在两次单击 commandButton 后更新
- swift - Swift 以编程方式添加本地联系人容器
- c++ - 如何将整数向量转换为 2 位十六进制并按顺序添加到 c 字符串?