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

Clarification Needed on Using Func with ScriptInjection #178

Open
bogdaniolu97 opened this issue Sep 23, 2024 · 2 comments
Open

Clarification Needed on Using Func with ScriptInjection #178

bogdaniolu97 opened this issue Sep 23, 2024 · 2 comments

Comments

@bogdaniolu97
Copy link

I’ve encountered difficulties using the Func property from ScriptInjection and haven’t been able to get it working. Any guidance or examples would be greatly appreciated.

` string InjectScriptAction()
{
return "function colorRed2(){console.log("colorRed3"); document.body.style.backgroundColor = 'red'; }";
}
var scriptInjection = new ScriptInjection

{
Target = new InjectionTarget

{
    TabId = tab.Id.Value,
},
Args = new List<object>() { "firstArgument" },
Func = () => InjectScriptAction(),

};
var result = await WebExtensions.Scripting.ExecuteScript(scriptInjection);`

manifest.json:
{
"manifest_version": 3,
"name": "Browser Extension With Blazor Extension",
"description": "My browser extension built with Blazor WebAssembly",
"version": "0.1",
"permissions": [
"activeTab",
"tabs",
"storage",
"webRequest",
"bookmarks",
"cookies",
"downloads",
"notifications",
"webNavigation",
"contextMenus",
"scripting"
],
"optional_permissions": [
"topSites"
],
"optional_host_permissions": [
"https:///",
"http:///"
],
"host_permissions": [
"http:///",
"https:///"
],
"background": {
"service_worker": "content/BackgroundWorker.js",
"type": "module"
},
"action": {
"default_popup": "popup.html"
},
"options_ui": {
"page": "options.html",
"open_in_tab": true
},
"content_security_policy": {
"extension_pages": "script-src 'self' 'wasm-unsafe-eval'; object-src 'self'"
},
"content_scripts": [
{
"matches": ["<all_urls>"]
}
],
"web_accessible_resources": [
{
"resources": [
"framework/",
"content/
",
"js/*.js"
],
"matches": [ "<all_urls>" ]
}
]
}

@mingyaulee
Copy link
Owner

The script injection feature might not be suitable to be executed from dotnet because dotnet is not available in the injected window. I haven't tested it personally yet, but it looks like it would be better to separate the injection into another JS file and use the Files property to inject the scripts.

@mingyaulee
Copy link
Owner

Also, evaluating JS in string is completely blocked by manifest v3, so your code that returns JS string will just be a string and will not be executed.

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