Can't inject JS Script before <head>

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Can't inject JS Script before <head>

Postby finder2 » Sun Aug 27, 2023 10:38 am

I need to add JS sripts from Chrome extension after <html> and before <head> to replicate the functionality of some extensions.
OnWebKitInitialized doesn't work for my JS code on my client and i can't use OnContextCreated because document.documentElement is null.
How can I inject JS script before <head>?
Where should I intercept it?
finder2
Mentor
 
Posts: 51
Joined: Fri Jun 13, 2014 1:33 am

Re: Can't inject JS Script before <head>

Postby KatrinaS » Sun Aug 27, 2023 11:42 am

Use one of the mechanisms available to Intercept the request such as in request handler, resource handler or similar, download it yourself 'in the raw', add what you need to the html then pass that new html to the browser? :)
KatrinaS
Mentor
 
Posts: 83
Joined: Tue Jul 04, 2023 6:30 pm

Re: Can't inject JS Script before <head>

Postby magreenblatt » Sun Aug 27, 2023 1:01 pm

You can use CefResponseFilter to modify the HTML contents on the fly.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Can't inject JS Script before <head>

Postby finder2 » Mon Aug 28, 2023 1:20 am

Thank you guus, for pointing me out. Hope it will work with JS IIFE functions.
finder2
Mentor
 
Posts: 51
Joined: Fri Jun 13, 2014 1:33 am

Re: Can't inject JS Script before <head>

Postby KatrinaS » Mon Aug 28, 2023 11:05 pm

I'm unsure if JavaScript in this particular position is going to be interpreted, it's not really a well formed document, but that said browsers in general are pretty fault / form tolerant so good luck with it :)
KatrinaS
Mentor
 
Posts: 83
Joined: Tue Jul 04, 2023 6:30 pm

Re: Can't inject JS Script before <head>

Postby finder2 » Tue Aug 29, 2023 2:21 pm

KatrinaS wrote:I'm unsure if JavaScript in this particular position is going to be interpreted, it's not really a well formed document, but that said browsers in general are pretty fault / form tolerant so good luck with it :)

Chrome places its extension with "document_start" right after <head> when i investigated it.
finder2
Mentor
 
Posts: 51
Joined: Fri Jun 13, 2014 1:33 am

Re: Can't inject JS Script before <head>

Postby finder2 » Thu Nov 02, 2023 3:59 pm

Finally i figured out how to add a JS code to html on the fly, but when I open, for example, google.com it says

Code: Select all
[Report Only] Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'nonce-7FHIekf3Jv342WbLuHw_8Q' 'strict-dynamic' 'report-sample' 'unsafe-eval' 'unsafe-inline' https: http:". Note that 'unsafe-inline' is ignored if either a hash or nonce value is present in the source list.


I tried to run CEF with
Code: Select all
--disable-web-security=1

to disable CSP, but it didn't help

Any tips, guys?

Another questions...
Can I intercept JS functions from C++ somewhere or can i find a start point in CEF where JS starts implementing its scripts?
finder2
Mentor
 
Posts: 51
Joined: Fri Jun 13, 2014 1:33 am

Re: Can't inject JS Script before <head>

Postby KatrinaS » Thu Nov 02, 2023 7:33 pm

Having no idea what / how you are tinkering with (either online, local or whatever) try enabling as much local file access as possible such as adding... disable-site-isolation-trails, allow-universal-access-from-files, allow-file-access-from-files etc etc,, in whatever language you are using's equivalent to CefAppHandlerAdapter / onBeforeCommandLineProcessing :)

As for the last question. Probably, but all of that's a headache (for me anyway) :lol:
KatrinaS
Mentor
 
Posts: 83
Joined: Tue Jul 04, 2023 6:30 pm

Re: Can't inject JS Script before <head>

Postby finder2 » Sun Nov 05, 2023 2:14 pm

Seems i was able to inject code with JS MutationObserver, i create MutationObserver on OnContextCreated and inject code when DOM is building.
finder2
Mentor
 
Posts: 51
Joined: Fri Jun 13, 2014 1:33 am

Re: Can't inject JS Script before <head>

Postby KatrinaS » Mon Nov 06, 2023 7:57 pm

Oh right,, never heard of it :D You C/++ guys have some good toys, makes me think my toys are sh*t :lol:
KatrinaS
Mentor
 
Posts: 83
Joined: Tue Jul 04, 2023 6:30 pm


Return to Support Forum

Who is online

Users browsing this forum: Google [Bot] and 206 guests

cron