How to call a function in javascript for google chrome extension?

16 views Asked by At

I am learning programming and I just started to create a simple google chrome extension.

I am trying to call a function from another function, but I get this error from chrome console.

Uncaught ReferenceError: check is not defined at alertMessage (:2:5) at :5:5

My program starts by clicking my tab on context menu. Here is my code

chrome.runtime.onInstalled.addListener(function (details) {

    const parent = chrome.contextMenus.create({
      id: "alert",
      title: "alertChrome",
      contexts: ["all"],
    });
  });
  
  chrome.contextMenus.onClicked.addListener((info, tab) => {
    switch (info.menuItemId) {  
      case "alert":
        chrome.scripting.executeScript({
          target: { tabId: tab.id },
          function: alertMessage,
        });
        break;
      }
  });
  
  
  function alertMessage() {
    if(check){
    alert('Hello World');
    }
  }
  
  function check(){
    return true;
  }

Why function check() cannot be called from alertMessage? Is there a way to make this work?

I tried running on google chrome console (pasting the code above on console) and it worked, so I believe it is a problem caused by chrome. I did a lot of research, but I could not find any article that mentions about this simple problem, so I think I am missing something basic.

0

There are 0 answers