Note that there are some explanatory texts on larger screens.

plurals
  1. POJava-script, Google chrome extension: Undefined class variable, in method referenced by callback
    primarykey
    data
    text
    <p>I am trying to make Google Chrome extension. </p> <p>For the beginning, I want to create javascript class, that represents the active browser tab and let me work with that HTML source. It should be used like this: </p> <pre><code>var currentTab = new CurrentTab(); currentTab.requestHtml(function(html){ // `html` contents active tab HTML }); </code></pre> <p>I googled, and I found that HTML of active tab can't be getted directly to extension's popup. But I can pass my own javascript to tab, that respons his HTML to my extension through <code>chrome.extension.sendRequest()</code>. So, my <code>CurrentTab</code> class look like this: </p> <pre><code>var CurrentTab = function(){ this.listeners = { 'requestHtml': {} }; chrome.extension.onRequest.addListener(this._processListener); }; CurrentTab.prototype = { requestHtml: function(callback){ var actionKey = Math.ceil(Math.random() * 10000); // get random identifier for this callback this.listeners['requestHtml'][actionKey] = callback; chrome.tabs.executeScript(null, { code: 'console.log("SendContent.js");' + 'chrome.extension.sendRequest({' + ' action: "' + actionKey + '",' + ' host: document.location.hostname,' + ' content: document.getElementsByTagName("html")[0].outerHTML' + '}, function(response){});' }); }, _processListener: function(request, sender, sendResponse){ /*25.*/console.log(this.listeners); // `this.listeners` is 'undefined' ??? if (this.listeners['requestHtml'][request.action]) { this.listeners['requestHtml'][request.action](request.content); delete this.listeners['requestHtml'][request.action]; } } }; </code></pre> <p>The problem is at line 25 of this class. Though the <code>_processListener</code> method is part of <code>CurrentTab</code> class, variable <code>this.listeners</code> is undefined here, when this method is called via callback. </p> <p>Please, how can I fix this and why it is happening? Thanks. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload