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

Mention the need to manually add unlisted script into web_accessible_resources if it's being injected. #536

Closed
KraXen72 opened this issue Mar 9, 2024 · 3 comments
Labels

Comments

@KraXen72
Copy link

KraXen72 commented Mar 9, 2024

Feature Request

After 2 hours of debugging, I've figured out that for an unlisted script to be injectable into the page it has to be declared in the manifest, like so:

// See https://wxt.dev/api/config.html
export default defineConfig({
	manifest: {
		web_accessible_resources: [
			{ resources: ["page.js"], matches: ["<all_urls>"] }
		]
	}
});

it would be good to mention this in the unlisted scripts docs or add it there automatically.

Is your feature request related to a bug?

N/A

What are the alternatives?

N/A

Additional context

This is my setup:

// page.ts
export default defineUnlistedScript(() => {
	console.log('unlisted script!')
})
// content.ts
export default defineContentScript({
	matches: ['<all_urls>'],
	main() {
		let pageScriptElem: HTMLScriptElement | null = null;
		
		browser.runtime.onMessage.addListener(
			async function (request, sender, sendResponse) {
				if (request.cmd && request.cmd === 'process') {
					if (pageScriptElem !== null) pageScriptElem.remove();

					pageScriptElem = document.createElement("script")
					pageScriptElem.src = browser.runtime.getURL("/page.js")
					pageScriptElem.type = 'text/javascript'
					document.head.appendChild(pageScriptElem)
				}
			}
		);
	}
});

Let me know if i'm misunderstanding how one should inject scripts like these

@aklinker1
Copy link
Collaborator

Nope, this looks correct. I'll update the docs.

@KraXen72
Copy link
Author

it would also maybe be worthwhile to add something like an accessible: boolean flag to unlisted scripts, that automatically adds them to the web-accessible resources for some given urls. or some other way to automate it. but if it's too much work, a mention in the docs works too.

@aklinker1
Copy link
Collaborator

it would also maybe be worthwhile to add something like an accessible: boolean flag to unlisted scripts, that automatically adds them to the web-accessible resources for some given urls. or some other way to automate it. but if it's too much work, a mention in the docs works too.

Hmm, for now, I'm gonna pass on this. In addition to #379, I'll create a new issue to supporting adding more manifest related options to the entrypoints.

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

No branches or pull requests

2 participants