Skip to content

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

@KraXen72

Description

@KraXen72

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions