0

I am working on a chrome extension that takes a name and a second name from user, and replaces all instances of name with second name on the tab the user is on. The logic is really easy, however, getting the input from the user and using it on the page is extremely challenging.

This is what I have:

Manifest.json:

{
  "manifest_version": 3,
  "name": "Background Changer",
  "version": "1.0.0",
  "description": "Does stuff with the background.",
  "action": {
    "default_title": "lol",
    "default_popup": "index.html"
  },
  "permissions": ["scripting", "tabs", "activeTab"],
  "host_permissions": ["<all_urls>"]
}

index.html

 <h2>Dead Name</h2>
  <input type="text" id="deadName">

  <h2>Real name</h2>
  <input type="text" id="realName">

  <input type="submit" id="submitButton">
    <script src="popup.js"></script>

popup.js

let realName = document.getElementById("realName")
let deadName = document.getElementById("deadName")

document.getElementById("submitButton").addEventListener('submit', injectJS);

const tabId = getTabId();
function injectJS(){
    chrome.scripting.executeScript(
        {
          target: {tabId: tabId, allFrames: true},
          files: ['content.js'],
        },
        () => {});
}

content.js

alert("LOL")

Simply put, this code is meant to be a test of injecting Javascript from the extension pop-out to the tab the user is on. Despite me attaching the event listener, and clicking on it, the alert does not appear.

Midas
  • 1
  • 2
  • There's no server for the extension page, which is just a local file, so replace the submit input with a simple button. Also pass the values via `args` as shown in the [documentation](https://developer.chrome.com/docs/extensions/reference/scripting/#runtime-functions). – wOxxOm Mar 02 '22 at 23:47
  • @wOxxOm I did that, but I'm getting the errors: getTabId is not defined at popup.js:6:15 popup.js:10 Uncaught ReferenceError: Cannot access 'tabId' before initialization at HTMLInputElement.injectJS – Midas Mar 02 '22 at 23:59
  • The examples in the documentation are all broken :-( Use [this example](https://stackoverflow.com/a/67227376). – wOxxOm Mar 03 '22 at 00:14

0 Answers0