Run chrome addon only against https://www.example.com/

Table of contents

Yes, you can modify background.js to ensure the script runs only on https://www.example.com/. Here’s the updated version:

console.log("Background service worker started");

// Listen for the toolbar button click in the background script
chrome.action.onClicked.addListener((tab) => {
  if (tab.url && tab.url.startsWith("https://www.example.com/")) {
    console.log("Toolbar icon clicked on example, executing content scripts...");

    // Inject both parts of the split content script
    chrome.scripting.executeScript({
      target: {tabId: tab.id},
      files: ['contentScriptPart1.js', 'contentScriptPart2.js']  // Inject both scripts
    }, () => {
      // Once the content scripts are injected, send the message
      chrome.tabs.sendMessage(tab.id, { action: "triggerAddonClick" });
      console.log("Message sent to content scripts");
    });
  } else {
    console.log("Addon 11. Ignored: Not on LinkedIn.");
  }
});

What’s changed:

  1. Added a URL check – The script will execute only if the tab’s URL starts with https://www.example.com/.

  2. Prevents execution on other sites – If the user clicks the addon icon outside LinkedIn, it will log "Addon 11. Ignored: Not on example." and do nothing.

This keeps it strictly limited to LinkedIn without modifying manifest.json or content scripts. 🚀 Let me know if you need adjustments!