1

I am trying to build a chrome extension. My content-script is a javascript class with set of functions.

myClass = class {
 constructor() {
   console.log('Script loaded');
  }

 get one() {}
 get two() {}
 get three() {}

}

obj = new myClass();

When I click on my chrome extension, the idea is to load above javascript and I should be able to call all the methods using obj. While I am getting 'Script loaded' message from the constructor in the console, when I try to call the methods using obj, I am getting below error.

obj is not defined.

If I copy-paste the entire content script in the console and try yo call the class methods using obj, it's working without any issues. what am I doing wrong here?

Manifest.json

    {
    "manifest_version":2,
    "name":"ext1",
    "description":"some desc",
    "version":"1.0.1",
    "background": {
    "scripts": ["js/background.js"],
    "persistent": true
     },

     "permissions":[
     "contextMenus",
     "activeTab"
     ]

   }

background.js

var contextMenus = {};

  contextMenus.ext1 =
  chrome.contextMenus.create(
    { "title": "some title" },
    function () {
        if (chrome.runtime.lastError) {
            console.log(chrome.runtime.lastError.message);

        }
    }
  );

chrome.contextMenus.onClicked.addListener(contextMenuHandler);

function contextMenuHandler(info, tab) {
 if (info.menuItemId === contextMenus.ext1) {
  
    chrome.tabs.executeScript({
        file: 'js/ext1.js'
    });
  

 }
}
RNimmagadda
  • 11
  • 1
  • 10

1 Answers1

0

Maybe something is wrong with context. Can you try to assign obj and myClass to block scope variables let or const. Example:

const myClass = class {
 constructor() {
   console.log('Script loaded');
  }

 get one() {}
 get two() {}
 get three() {}

}

const obj = new myClass();
amaj
  • 270
  • 1
  • 2
  • 8