Skip to content

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

Closed

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

Activity

  1. aklinker1 commented on Mar 9, 2024

    @aklinker1
    Member

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

  2. KraXen72 commented on Mar 10, 2024

    @KraXen72
    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.

  3. aklinker1 commented on Mar 12, 2024

    @aklinker1
    Member

    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.

  4. added a commit that references this issue on Mar 12, 2024
    121b521
  5. mundane799699 commented on Jul 24, 2024

    @mundane799699
    Contributor

    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

    thank you, it's so useful tp help me to solve the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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