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

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

问题描述

当我们点击地址栏(出现URL的地方)旁边列出的扩展按钮时, popup.html 出现。 (当然,根据 manifest.json



当我点击lastPass时,弹出窗口即刻显示,但当我点击我的自定义扩展名(包含popup.html时除外),鼠标图标变为加载1-2秒&然后弹出窗口打开。



有些人在挖掘为什么我的弹出窗口太慢,google-group有一些像

  window.load = setTimeout(activate,0); 

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



请帮忙弄清楚为什么我的扩展弹出很慢,尽管代码中没有包含弹出窗口(chrome-extensions开发中的初学者)。



更新



manifest.json
$ b

  {
manifest_version:2,

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

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

popup.html

 <!doctype html> 
< html>
< head>
< / head>
< body>
< div>
< label>输入HR密码< / label>
< input type ='password'id ='passwd'/>
< button id ='evaluateResults'>评估< /按钮>
< ul id ='results'style ='width:100px;'>

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

popup.js

  var totalCorrect = 0,totalWrong = 0; 
$ b $ 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(不正确的密码);
}
});

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文档中没有解释(或者至少我没有找到它),所以它更像是一种侥幸,但似乎有效。



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



<在清单中添加如下内容:

{// ...background:{page: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天全站免登陆