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

Support Firefox add-ons execution environment #44

Closed
EnixCoda opened this issue May 25, 2020 · 9 comments
Closed

Support Firefox add-ons execution environment #44

EnixCoda opened this issue May 25, 2020 · 9 comments

Comments

@EnixCoda
Copy link

EnixCoda commented May 25, 2020

Hi @falsandtru

I'm the maintainer of a GitHub file tree extension, Gitako, which depends on PJAX lib to work properly. Thank you for implementing this great new generation of PJAX lib, I found it working much better than the one I was using before! However, while it worked very well in Chrome, I encountered an cross-origin issue when trying to use it in Firefox - Not allowed to define cross-origin object as property on [Object] or [Array] XrayWrapper, which crashes my extension then.

image

What I've done

I've tried to fix it by myself, but couldn't figure it out after many hours of work. So I decided to ask for your help.

The reason it happen is, according to MDN, add-on for Firefox runs in a context other than the context that webpage js runs in.

A content script is a part of your extension that runs in the context of a particular web page (as opposed to background scripts which are part of the extension, or scripts which are part of the web site itself, such as those loaded using the <script> element).

BTW, add-on to Firefox is exactly what extension is to Chrome, they are just different names.

So that some operations happened inside pjax-api (e.g. assigning variables to properties of window.location) triggered cross-origin issue because window is not from add-ons context.

But my add-on used https://github.com/MoOx/pjax and it worked well even in Firefox. So I think PJAX mechanism is not technically unavailable in Firefox. What we need is few minor changes in pjax-api, like switching from passing objects to assigning object properties specifically to prevent passing objects cross-origin.

How to reproduce it

If you'd like to help (it would be very kind of you!), please continue reading about how to reproduce the issue on your local environment.

In case you might not be familiar about browser extension development, I've setup a project for you - https://github.com/EnixCoda/pjax-api-firefox-demo

  1. Please clone it and install dependencies with yarn.

  2. Run yarn watch to emit files for debug and then run yarn debug-firefox in another terminal session to debug in Firefox. You should see a Firefox window pop out.

  3. Go visit some webpage. I'd suggest https://nobody.github.io as it'll take you to a clean 404 page which has no much JS files.

  4. Open dev console and you should see App is running! like below, which indicates the app works by default.
    image

  5. Then go to src/content_script.ts, uncomment those commented lines. Now pjax-api is imported. Refresh browser page. The App is running! log won't appear now. Because app crashed in importing pjax-api, before it executed to where the log statement is.

  6. Now go to Debugger tab, check Pause on exceptions and Pause on caught exceptions under breakpoint pane. Refresh page again. You should see exactly what I showed you in the first screenshot - Not allowed to define cross-origin object as property on [Object] or [Array] XrayWrapper.

  7. Then you should be able to see what is going wrong.


I'd be very grateful if you either take a look into the issue or guide me about how to fix it myself. Thank you very much!

@falsandtru
Copy link
Owner

I fixed cross-origin access only about window.history.state. Is there another illegal access?

e.g. assigning variables to properties of window.location

If you actually get that error, can you also show me it and its call stack?

@EnixCoda
Copy link
Author

EnixCoda commented May 26, 2020

Thank you for fast responding!

Here are screenshots of each level of the call stack, top down.

image

image

image

image

It looks like that the issue happens when assigning position to window.history.state?

@falsandtru
Copy link
Owner

OK, it should be fixed at v3.31.1. Can you confirm it?

@EnixCoda
Copy link
Author

It's not :(

The issue occurs in savePosition while your changes updated mutations on state in functions except it.

savePosition is still mutating on cross origin state.

export function savePosition(): void {
void window.history.replaceState(
extend<Pick<State, State.position>>(
window.history.state || {},
{
position: {
top: window.pageYOffset,
left: window.pageXOffset
}
}),
document.title);

@EnixCoda
Copy link
Author

EnixCoda commented May 26, 2020

Sorry, I missed later commits in which you fixed savePosition.

But the issue does still exist after I updated pjax-api. I'll remove local cache and give another try ASAP.

@EnixCoda
Copy link
Author

The cross-origin issue has been fixed successfully! 🍻

...and another occurred - TypeError: can't prevent extensions on this proxy object

It is an issue of URL.

Call stacks

image
image
image
image
image
image

The direct reason

image

I did some dig on it (see the console). It seems that the reason is Object.freeze failed on URL instances.

@falsandtru
Copy link
Owner

OK, I'll remove Object.freeze.

@falsandtru
Copy link
Owner

Fixed at v3.31.2.

@EnixCoda
Copy link
Author

It works! Thank you so much!

Now I can release next version of Gitako and users will have better user experiences!

If you use GitHub frequently, please give a try :P

https://github.com/EnixCoda/Gitako

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants