Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

my chrome extension popup opens after a few seconds, it's slow compared to other extensions

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);
});
like image 636
coding_idiot Avatar asked Oct 09 '14 11:10

coding_idiot


People also ask

Why do extensions slow down Chrome?

Extensions are one of the best bits about Chrome, but each one adds bloat to the browser and therefore can eat up more of your computer's memory and slow it down.

What is faster Chrome extension?

1. FasterChrome (Chrome): Speed Up Browsing by Smart Preloading. FasterChrome is an extension that claims to be smart enough to figure out which page you are about to click and starts loading it before you click.


1 Answers

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.

like image 184
Mario Mucalo Avatar answered Oct 11 '22 10:10

Mario Mucalo