Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Content Script Unable to Access window.require for Website Module Integration #1149

Closed
2 of 3 tasks
pedroota opened this issue Dec 24, 2024 · 0 comments
Closed
2 of 3 tasks
Labels
bug Something isn't working

Comments

@pedroota
Copy link

What happened?

I'm developing a browser extension that needs to interact with specific modules from a website using window.require. The extension attempts to load and manipulate these modules through a content script, but the script is unable to detect when window.require becomes available.

Current Implementation:

  • I've created a content script that implements a polling mechanism to wait for window.require to become available
  • The script uses setInterval to check for window.require every 100ms
  • Once detected, it should initialize the extension with the required modules
  • The script runs either on document complete or window load

Issue:

The polling mechanism never detects window.require, despite the website eventually loading it. The console continuously logs "Waiting for window.require..." without ever succeeding.

Expected Behavior:

  • The content script should detect when window.require becomes available
  • Once available, it should successfully import the website's modules for manipulation
function initialize(require: NodeRequire) {
**// I can now use inside Content Script UI window.store.Chat**
window.store.Chat = require("WAWebMediaMmsV4Upload") 
}

const initializeExtension = (require: NodeRequire) => {
  initialize(require)
}

function waitForRequire(callback: (require: NodeRequire) => void) {
  const interval = setInterval(() => {
    if (!!window.require) {
      clearInterval(interval)
      callback(window.require)
    } else {
      console.log("Waiting for window.require...")
    }
  }, 100)
}

const waitForWhatsAppLoad = () => {
  waitForRequire((require) => initializeExtension(require))
}

if (document.readyState === "complete") {
  waitForWhatsAppLoad()
} else {
  window.addEventListener("load", (event) => {
    waitForWhatsAppLoad()
  })
}

Version

Latest

What OS are you seeing the problem on?

MacOSX

What browsers are you seeing the problem on?

Chrome

Relevant log output

No response

(OPTIONAL) Contribution

  • I would like to fix this BUG via a PR

Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I checked the current issues for duplicate problems.
@pedroota pedroota added the bug Something isn't working label Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant