google-chrome - 我的 chrome 扩展程序弹出窗口会在几秒钟后打开,与其他扩展程序相比,它的速度很慢

标签 google-chrome google-chrome-extension popup

当我们单击地址栏(出现 URL 的位置)旁边列出的扩展程序按钮时,就会显示相应扩展程序的 popup.html。 (当然,根据manifest.json)

当我点击lastPass时,弹出窗口立即出现,但是当我点击我的自定义扩展(仅包含popup.html)时,鼠标图标变为加载状态并持续1-2秒,然后弹出窗口打开。

是否深入研究了为什么我的弹出窗口如此缓慢,谷歌组有类似的东西

window.load=setTimeout(activate,0);

无法找到任何相关文档或工作示例。

请帮助找出为什么我的扩展弹出窗口如此慢,尽管代码只包含弹出窗口(chrome扩展开发的初学者)。

更新

ma​​nifest.json

{
  "manifest_version": 2,

  "name": "Sample Name",
  "description": "Sample Descriptoin",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "permissions": [
    "<all_urls>"
  ]
}

popup.html

<!doctype html>
<html>
    <head>
    </head>
    <body>
        <div>
            <label>Enter HR Password</label>
            <input type='password' id='passwd'/>
            <button id='evaluateResults'>Evaluate</button>
            <ul id='results' style='width:100px;'>

            </ul>
        </div>
        <script src='popup.js'></script>
    </body>
</html>

popup.js

var totalCorrect=0, totalWrong=0;

document.getElementById('evaluateResults').addEventListener('click',function(){
    var passwd=document.getElementById('passwd').value;
    if(passwd==='123'){     
        var strCode="var scriptOptions = {role:'blank'};";
        chrome.tabs.executeScript(null, {code: strCode,allFrames:true}, function(){
            chrome.tabs.executeScript(null, {file: "content_script_evaluate.js",allFrames:true});       
        });
    }else{
        alert("Incorrect Password");
    }
});

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    console.log(request);
    var ul=document.getElementById('results');
    var li=document.createElement('li');
    li.innerHTML=request.testName+" - "+(request.testResult?"Correct":"Wrong");
    ul.appendChild(li);
});

最佳答案

有效的方法是添加一个空的背景页面。 Google 文档中没有对此进行解释(或者至少我没有找到它),因此这更像是侥幸,但似乎有效。

这个想法是,当您进入页面时(因此在您点击之前),插件会加载一次,而不是在每次点击时一遍又一遍地重新加载。

在 list 中添加如下内容:

{
  //...
  "background": {
    "page": "bg.html"
  }
  //...
}

bg.html 可以只是一个空的 HTML 文件:

<html>

</html>

再次 - 从未找到明确的链接或资源来解释为什么应该这样做,我不确定这是最佳实践,但它确实对我有用。

关于google-chrome - 我的 chrome 扩展程序弹出窗口会在几秒钟后打开,与其他扩展程序相比,它的速度很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26276815/

相关文章:

javascript - Chrome 扩展 : writing content into a dynamic iframe created in a sandboxed environment

javascript - 在输出之前从 src 更改 javascript?

python-3.x - Kivy:弹出窗口只能有一个小部件作为内容

javascript - 如何使这个 JavaScript 弹出窗口模式化?

google-chrome - 如何缩小 chrome 扩展程序的弹出窗口

javascript - Chrome onMessage 监听器没有被调用

CSS:尝试将图像垂直居中放置在 div 内...Chrome、Safari 问题

javascript - Uncaught ReferenceError : X is not defined

c# - 如何从代码后面弹出可见并捕获其事件?

c# - 为什么 Response.Redirect() 在 Chrome 中有效,但在 IE 中无效?