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

Chrome Extension: window is not defined for background scripts #686

Closed
subzero10 opened this issue Jan 20, 2022 · 11 comments
Closed

Chrome Extension: window is not defined for background scripts #686

subzero10 opened this issue Jan 20, 2022 · 11 comments
Assignees
Labels
js @honeybadger-io/js

Comments

@subzero10
Copy link
Member

Looks like window is not available in background scripts.

Bug first reported here.

@joshuap
Copy link
Member

joshuap commented Jan 21, 2022

Hmmm. I would love to avoid a situation where we have to guard access to window in the browser client. 🤔

That said, something similar came up w/ someone who was trying to integrate with a Cloudflare worker (which is also on my integration wishlist).

@christianopaets
Copy link

christianopaets commented Jan 26, 2022

Instead of window we can use self. I have tried to to use smth like global.window = self and the error disapeared. But there is another problem)) document also doesn't exists.

And service worker uses only fetch API

@christianopaets
Copy link

christianopaets commented Oct 4, 2022

@subzero10 any updates on this?

@subzero10
Copy link
Member Author

Hey @christianopaets, I haven't looked into this since January. It's in my todo list, I will try to bump it up.

@lbaron-ut
Copy link

It would be really useful when working with a Browser extension as manifest v3 is becoming the norm...

@zhanfengh
Copy link

Hello, we run a popular Chrome extension. And Honeybadger is an essential component of error reporting for us.

Google Chrome has mandated all Chrome extensions to be migrated to Manifest V3 and one of the notable changes of MV3 is that it replaces background pages with service workers. The DOM and XMLHttpRequest() are not available in service workers, which makes Honeybadger to error out.

We tried to fork and patch the library to make it compatible with service workers by replacing window with self when it's possible and XMLHTTPRequest() with fetch(). But we kept running into issues with a lot of different window/document calls within the library and couldn't figure out a clean way to fix them all.

It would be nice if you can look into this issue and make Honeybadger compatible with Manifest V3 soon as Google Chrome will not accept any extensions without MV3 after June 2023 and will actively remove them after Jan 2024. https://developer.chrome.com/blog/more-mv2-transition/

@subzero10
Copy link
Member Author

Hey @zhanfengh, thank you for your insights and feedback. Then, I guess I should look into this before the EOY!
cc @joshuap

@guest271314
Copy link

You can alternatively substitute globalThis for self console.assert(globalThis === self).

It looks like XMLHttpRequest is referenced in /src/browser/transport.ts, src/browser/integrations/breadcrumbs.ts, and test.js.

For transport.ts you can do something like this, substituting arrayBuffer() for text(), json(), blob(), or formData() where appropriate

    return new Promise(async (resolve, reject) => {
      try {
        const headers = new Headers();
        if (Object.keys(options.headers || []).length) {
          for (const i in options.headers) {
            if (typeof options.headers[i] !== 'undefined') {
              headers.set(i, String(options.headers[i]))
            }
          }
        }
        const x = await fetch(options.endpoint, {
          method: options.method, 
          body: payload ? JSON.stringify(sanitize(payload, options.maxObjectDepth)) : ''
          headers
        });
        resolve({ statusCode: x.status, body: await x.text() })
      } catch (err) {
        reject(err)
      }
    })

@subzero10
Copy link
Member Author

Thanks @guest271314, I will try this out!

@KonnorRogers
Copy link
Collaborator

KonnorRogers commented Nov 23, 2022

Alright, theres a ton of calls beyond the transport.ts file mentioned, thats probably just the first code path that gets hit. we make a lot of calls to window objects and XMLHTTPRequest, I'm gonna keep plugging through it, but heres an initial PR for what the transport.ts would be refactored to, PR is WIP as it'll be a fairly large refactor.

#958

@subzero10
Copy link
Member Author

subzero10 commented Dec 12, 2022

Hey all, we just released v4.8.0 which should support running honeybadger in non-dom environments (chrome extension, background workers)!
I'm closing this ticket, but don't hesitate to open again if you find issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js @honeybadger-io/js
Projects
None yet
Development

No branches or pull requests

7 participants