gmailr icon indicating copy to clipboard operation
gmailr copied to clipboard

Random Error: Uncaught ReferenceError: $LAB is not defined

Open arbet opened this issue 12 years ago • 3 comments

I'm seeing the following error in my console at random times:

Uncaught ReferenceError: $LAB is not defined

It pops from time to time, but most of the time the page is loading properly. Any idea what might be causing this?

arbet avatar Oct 17 '12 09:10 arbet

I think the way loading happens in bootstrap.js is the culprit. It relies on an asynchronous loading technique to inject both lab.js and init.js, and so it's possible for init.js to be loaded before lab.js is executed:

https://github.com/jamesyu/gmailr/blob/92330b0ca477697e87f56c9518903cb49dbf660d/chrome/lib/bootstrap.js#L40

One thing worth trying is replacing those loadScript lines with:

        // Load the initialization scripts
        chrome.extension.sendMessage({ "action": "load_scripts", "payload": [
            "lab.js",
            "init.js"
        ]});

Then in a background.js file, add something like:

chrome.extension.onMessage.addListener(
  function (request, sender, sendResponse) {
    if (request.action === 'load_scripts') {
            var scripts = request.payload;
            for (var i = 0, ilen = scripts.length; i < ilen; i++) {
                chrome.tabs.executeScript(null, { "file": scripts[i] });
            }
            break;
    }
  });

I'd submit this as a pull request, but I wanted to check two things first:

  • whether there's a less complicated solution (e.g. http://stackoverflow.com/a/2880147/69697 )
  • whether there are any security concerns introduced here -- e.g. could files not belonging to the extension be injected here, could remote files be injected here...

bregenspan avatar Feb 13 '13 17:02 bregenspan

The way I got round this is at the very bottom of lab.js I added:

$LAB
.script(document.getElementById("init_path_gcgmailr_data").getAttribute('data-val'))
.wait();

In bootstrap.js I:

addData("init_path",        chrome.extension.getURL("lib/init.js"));

// and commented out 
//loadScript(chrome.extension.getURL("lib/init.js"));

this seemed to work for me

Cyapow avatar Nov 22 '13 15:11 Cyapow

I think to great extent I gave up on LAB and embedded my javascripts with the Rails asset pipeline into a single huge js. I think another reason this was required was because I may be racing to load with other Gmail plugins like Boomerang which were loading their JS ahead of my LAB's.

On Fri, Nov 22, 2013 at 7:54 AM, Cyapow [email protected] wrote:

The way I got round this is at the very bottom of lab.js I added:

$LAB.script(document.getElementById("init_path_gcgmailr_data").getAttribute('data-val')).wait();

In bootstrap I:

addData("init_path", chrome.extension.getURL("lib/init.js")); // and commented out //loadScript(chrome.extension.getURL("lib/init.js"));

this seemed to work for me

— Reply to this email directly or view it on GitHubhttps://github.com/jamesyu/gmailr/issues/11#issuecomment-29082934 .

mikedll avatar Nov 22 '13 20:11 mikedll