0

I'm trying to write my first browser extension for Chrome/Vivaldi but for some reason I can't seem to get it to write to the console log to assist in debugging another issue, which may be related.

Its supposed trigger a notification when you highlight a game and use the content menu button but its like it doesnt even run and so console log were added but none of them seem to get trigger according to the console.

For this post I needed to remove the apiKey

background.js

console.log("Creating context menu item...");
chrome.contextMenus.create({
  id: "isthereanydeal",
  title: "Look up on IsThereAnyDeal",
  contexts: ["selection"]
});
console.log("Context menu item created.");

chrome.contextMenus.onClicked.addListener(function(info, tab) {
  console.log("Context menu clicked");
  if (info.menuItemId === "isthereanydeal") {
    const gameName = decodeURIComponent(info.selectionText.trim().replace(/\s/g, '%20'));
    const apiKey = '~~enter api key here~~';
    const url = `https://api.isthereanydeal.com/v01/game/prices/?key=${apiKey}&plains=${gameName}&region=us`;
    console.log(`URL: ${url}`);

    fetch(url)
      .then(response => response.json())
      .then(data => {
        console.log("Data received from Isthereanydeal");
        const bestPrice = data.data[gameName].list[0].price_new;
        const discount = data.data[gameName].list[0].price_cut;

        const notificationOptions = {
          type: "basic",
          title: "Best Price on Isthereanydeal",
          message: `Game: ${gameName}\nBest Price: $${bestPrice}\nDiscount: ${discount}% off`
        };

        chrome.notifications.create(notificationOptions);
      })
      .catch(error => console.log(error));
  }
});

manifest.json

{
  "name": "Isthereanydeal Lookup",
  "version": "1.0",
  "description": "Lookup video game names on Isthereanydeal.com",
  "manifest_version": 3,
  "background": {
    "service_worker": "background.js"
  },
  "permissions": [
    "contextMenus",
    "tabs"
  ],
  "host_permissions": [
    "https://api.isthereanydeal.com/*"
  ]
}

Does anyone have suggestions on why I cant even write to the console with it?

Amos M. Carpenter
  • 4,848
  • 4
  • 42
  • 72
BrightHalo
  • 155
  • 2
  • 10
  • Does this answer your question? [google chrome extension :: console.log() from background page?](https://stackoverflow.com/questions/3829150/google-chrome-extension-console-log-from-background-page) – Sani Huttunen Feb 14 '23 at 06:39
  • Thank you but I tried chrome.extension.getBackgroundPage() is not a function that exists in manifest version 3 according to this error: Uncaught TypeError: chrome.extension.getBackgroundPage is not a function – BrightHalo Feb 14 '23 at 07:13
  • Since it's a Service worker you can see [these instructions](https://stackoverflow.com/questions/63024113/how-to-debug-chrome-extension-service-worker-for-manifest-v3) for debugging. – Sani Huttunen Feb 15 '23 at 03:35

0 Answers0