Skip to content

feat: autoMount content script UIs - #1210

Merged
aklinker1 merged 45 commits into
wxt-dev:mainfrom
1natsu172:auto-mount
Dec 7, 2024
Merged

aklinker1 merged 45 commits into
wxt-dev:mainfrom
1natsu172:auto-mount

Conversation

@1natsu172

@1natsu172 1natsu172 commented Nov 26, 2024 •

Copy link
Copy Markdown
Contributor

refer: #537

implementation

Basically the style of the existing implementation, the ui.remove method is internally split. Because what autoMount callback and what the user calls are different in behavior.

testing

Tested for all built-in UIs.

The API spec makes it a floating-promise, so I'm hacking to get over the asynchronous processing. We must call runMicrotasks after every dom change and floating-promise process. Otherwise, only the vitest process would proceed and result in a false positive test.

wxt-demo

Added new automount.content. The gif I posted on Discord, with a delay in between for directional purposes.

docs

It's pretty temporary, so feel free to fix it so that it's consistent with the other pages. The same is true for JSDoc, please correct any areas of concern.

@netlify

netlify Bot commented Nov 26, 2024 •

Copy link
Copy Markdown

✅ Deploy Preview for creative-fairy-df92c4 ready!

Name Link
🔨 Latest commit 0ad6be0
🔍 Latest deploy log https://app.netlify.com/sites/creative-fairy-df92c4/deploys/675433500cb6f40008858cd2
😎 Deploy Preview https://deploy-preview-1210--creative-fairy-df92c4.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@1natsu172
1natsu172 marked this pull request as ready for review November 26, 2024 16:40
@aklinker1

Copy link
Copy Markdown
Member

Also, seems like windows tests are broken?

@aklinker1

Copy link
Copy Markdown
Member

Hmm, the stop function returned from ui.autoMount won't really have a purpose anymore after this change, will it? Because it's the same thing as ui.remove? So is the solution just to remove the return value and tell people to use remove instead?

@aklinker1

aklinker1 commented Dec 1, 2024 •

Copy link
Copy Markdown
Member

Actually, maybe there is value in keeping the stop function. Maybe we should just tell people to call remove in the docs, and optionally mention that they can stop watching but keep the UI mounted if they want by calling stop.

@1natsu172

Copy link
Copy Markdown
Contributor Author

Hmm but now I feel it is better to stipulate the operation strictly. In the original Issue we had a conversation about covering only one scenario, and the once option also includes remove. It is less confusing to make the operation consistent.

@1natsu172

Copy link
Copy Markdown
Contributor Author

If we are confused, the users will be more confused.

@1natsu172

1natsu172 commented Dec 1, 2024 •

Copy link
Copy Markdown
Contributor Author

Also, seems like windows tests are broken?

It was an error that the document object was not found in the background, but there may be a flaky test. It passes now. 🤔

Edit:
I added a cleanup because the floating-promise may have been carried over to the next test.

@aklinker1 aklinker1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, then let's remove the return value since remove and stop are doing the same thing.

@aklinker1
aklinker1 dismissed their stale review December 3, 2024 17:08

out of date

@1natsu172

Copy link
Copy Markdown
Contributor Author

@aklinker1 Alright, I've integrated stopAutoMount into the remove method. I feel it's simpler.

@1natsu172
1natsu172 requested a review from aklinker1 December 6, 2024 09:47

@aklinker1 aklinker1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks for adding all those tests too!

@aklinker1

aklinker1 commented Dec 6, 2024 •

Copy link
Copy Markdown
Member

Hmmm background test is still failing. Why would the auto mount library be imported for that test?

@1natsu172

1natsu172 commented Dec 6, 2024 •

Copy link
Copy Markdown
Contributor Author

Hmm, I thought the cleanup was inadequate, but I guess not.

Actually, regardless of this PR (even in the main branch), getting Content script “test” context invalidated and Happy-dom errors in other tests. It might be related. 🤔 But why Windows?

Run pnpm run test ui

stderr | src/client/content-scripts/ui/__tests__/index.test.ts > Content Script UIs > mounted value > iframe > should set the mounted value based on the onMounted return value
DOMException [NotSupportedError]: Failed to fetch from "chrome-extension://test-extension-id/": URL scheme "chrome-extension" is not supported.
    at Function.validateSchema (file:///Users/1natsu/ghq/github.com/wxt-dev/wxt/node_modules/.pnpm/[email protected]/node_modules/happy-dom/src/fetch/utilities/FetchRequestValidationUtility.ts:120:10)
stdout | src/client/content-scripts/ui/__tests__/index.test.ts
[wxt] Content script "test" context invalidated
[wxt] Content script "test" context invalidated
[wxt] Content script "test" context invalidated
.
.

@1natsu172

Copy link
Copy Markdown
Contributor Author

Huh, if I look closely, it's a wxt-demo test file. And I reproduced it on my local macOS by running pnpm test with monorepo root. I'll investigate.

@1natsu172

Copy link
Copy Markdown
Contributor Author

@aklinker1 I figured out that if I commented out the useAppConfig here, it would pass. but……Why is my library code flowing in here? 🤷

console.log(useAppConfig());

@aklinker1

Copy link
Copy Markdown
Member

Ok, thanks for investigating. I'll check it out after work today

@aklinker1

aklinker1 commented Dec 7, 2024 •

Copy link
Copy Markdown
Member

I see, simple problem. useAppConfig is auto-imported from wxt/client, which includes the UI functions, so your library gets loaded, and it accesses document as soon as it's imported... I suppose useAppConfig should be imported from wxt/sandbox instead. Hmm... gonna have to wait for 0.20.0 for this, that's definitely a breaking change.

@1natsu172 this is caused because in @1natsu/wait-element, you're accessing document immediately on import (ie, as a side effect). To make it safe across different runtimes, I'd recommend using globalThis.document instead (which evaluates to undefined in environments without the global instead of throwing an error), or not accessing it directly until an API is called.

That said, this is WXT's fault, so I'll come up with a fix for it on my side. Just a compatibility recommendation.

@1natsu172

Copy link
Copy Markdown
Contributor Author

@aklinker1

which includes the UI functions, so your library gets loaded, and it accesses document as soon as it's imported

I see...thank you for the investigation.

To make it safe across different runtimes, I'd recommend using globalThis.document instead

I didn't expect it to be referenced outside of the browser environment, but you're right about cases like this one. I'll fix soon my library and publish it too.

@1natsu172

Copy link
Copy Markdown
Contributor Author

Okay, I added a commit updating upstream(my library). The test passes on my local.

@aklinker1

Copy link
Copy Markdown
Member

Awesome, I've got the WXT change working, but there's a bit more cleanup to do. But we can merge this PR!

@aklinker1
aklinker1 merged commit 8f71d80 into wxt-dev:main Dec 7, 2024
@s-bessing

Copy link
Copy Markdown

@aklinker1 @1natsu172 is it possible to add the element multiple times? If the anchor is present more the once then also the elements should be mounted to every element on the page. Is this possible?

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

Successfully merging this pull request may close these issues.

3 participants