几秒钟后我的 chrome 扩展弹出窗口打开,与其他扩展相比它很慢 [英] my chrome extension popup opens after a few seconds, it's slow compared to other extensions

查看:19
本文介绍了几秒钟后我的 chrome 扩展弹出窗口打开,与其他扩展相比它很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们点击extension button,列在地址栏(URL 出现的地方)旁边,相应扩展的popup.html 就会出现.(当然根据manifest.json)

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

对为什么我的弹出窗口这么慢做了一些挖掘,谷歌组有类似的东西

window.load=setTimeout(activate,0);

找不到任何相关文档或工作示例.

请帮助弄清楚为什么我的扩展弹出窗口如此缓慢,尽管代码只包含弹出窗口(chrome-extensions 开发的初学者).

更新

ma​​nifest.json

<代码>{清单版本":2,"name": "样本名称",描述":样本描述",版本":1.0",浏览器操作":{"default_icon": "icon.png",default_popup":popup.html"},权限":[<all_urls>"]}

popup.html

<!doctype html><html><头></头><身体>

<label>输入 HR 密码</label><输入类型='密码'id='passwd'/><button id='evaluateResults'>评估</button><ul id='results' style='width:100px;'></ul></div><script src='popup.js'></script></身体></html>

popup.js

var totalCorrect=0, totalWrong=0;document.getElementById('evaluateResults').addEventListener('click',function(){var passwd=document.getElementById('passwd').value;如果(密码==='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});});}别的{alert("密码错误");}});chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {控制台.log(请求);var ul=document.getElementById('results');var li=document.createElement('li');li.innerHTML=request.testName+" - "+(request.testResult?"Correct":"Wrong");ul.appendChild(li);});

解决方案

有用的是添加一个空的背景页面.这在 Google 文档中没有解释(或者至少我没有找到它),所以这更像是侥幸,但似乎有效.

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

在清单中添加如下内容:

{//...背景": {页面":bg.html"}//...}

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

<html></html>

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

When we click on the extension button, listed beside the address bar (where the URL appears), popup.html of the corresponding extension shows up. (of course, according to manifest.json)

When I click on lastPass, the popup appears instantly, but when I click on my custom-extension (contains nothing but popup.html), the mouse icon changes to loading for 1-2 seconds & then the popup opens up.

Did some digging on why my popup is so slow, the google-groups had something like

window.load=setTimeout(activate,0);

Unable to find any related documentation, or working sample.

Please help in figuring out why my extension popup is so slow, though the code contains nothing just the popup (beginner in chrome-extensions development).

Update

manifest.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);
});

解决方案

What worked for was to add an empty background page. This is not explained in the Google Documentation (or at least I did not find it), so it was more of a fluke, but seems to work.

The idea is that the plugin is loaded once when you come to the page (so before you even click), as opposed to being reloaded over and over again on each click.

In the manifest add something like:

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

And the bg.html can just be an empty HTML file:

<html>

</html>

Again - never found an explicit link or resource explaining why this should be done like this, and I am not sure it is the best practice, but it did work for me.

这篇关于几秒钟后我的 chrome 扩展弹出窗口打开,与其他扩展相比它很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆