How to run JS on page load started?

133 views
Skip to first unread message

Anton Smirnov

unread,
Sep 1, 2017, 1:42:47 AM9/1/17
to Chromium-dev
Hey, guys.

I'm working on some content filtering library integration and it requires some JS to be executed on page load.
It's not a spam, malware, etc - it just allows user to filter some undesired content.

For this i need to figure out how:
1) to catch "page/iframe load started" event.
URL is required for it as it will generate JS script body dynamically.

2) to launch JS
It seems that #1 happens in browser process and #2 happens in child process, is it correct?
Should i add some IPC messages?
Actually the script will subscribe to 'DOMContentLoaded' and 'load' events as it requires DOM to be built,
so probably if there is a way to subscribe to that events directly it will work too.

Any suggestions and starting points are highly appreciated. Thanks.

PhistucK

unread,
Sep 1, 2017, 4:12:34 AM9/1/17
to d...@antonsmirnov.name, Chromium-dev
Why do you need C++ for that? Extensions can already do this...


PhistucK

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev+unsubscribe@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/4ed07163-f901-4041-b1fd-81a004aac063%40chromium.org.

Anton Smirnov

unread,
Sep 4, 2017, 3:18:13 AM9/4/17
to Chromium-dev, d...@antonsmirnov.name
Hi.


On Friday, September 1, 2017 at 1:12:34 PM UTC+5, PhistucK wrote:
Why do you need C++ for that? Extensions can already do this...

The first reason is that extensions are not supported on mobile AFAIK.
The second, some browsers have similar feature as built-in killer feature so i'd like to provide an example how the library can be integrated in chromium and forks.
 

PhistucK

On Fri, Sep 1, 2017 at 8:42 AM, Anton Smirnov <d...@antonsmirnov.name> wrote:
Hey, guys.

I'm working on some content filtering library integration and it requires some JS to be executed on page load.
It's not a spam, malware, etc - it just allows user to filter some undesired content.

For this i need to figure out how:
1) to catch "page/iframe load started" event.
URL is required for it as it will generate JS script body dynamically.

2) to launch JS
It seems that #1 happens in browser process and #2 happens in child process, is it correct?
Should i add some IPC messages?
Actually the script will subscribe to 'DOMContentLoaded' and 'load' events as it requires DOM to be built,
so probably if there is a way to subscribe to that events directly it will work too.

Any suggestions and starting points are highly appreciated. Thanks.

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.

PhistucK

unread,
Sep 4, 2017, 3:38:58 AM9/4/17
to Anton Smirnov, Chromium-dev
Correct, mobile Chrome cannot run extensions.


PhistucK

To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev+unsubscribe@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/dce9911a-0071-48ae-b6a3-ba8fe14916d3%40chromium.org.

PhistucK

unread,
Sep 4, 2017, 3:39:10 AM9/4/17
to Anton Smirnov, Chromium-dev
I think WebView can inject scripts to pages.
So I suggest you try to trace it, from the API to the renderer code that does that, using cs.chromium.org.


PhistucK

Anton Smirnov

unread,
Sep 4, 2017, 7:12:14 AM9/4/17
to Chromium-dev, d...@antonsmirnov.name


On Monday, September 4, 2017 at 12:39:10 PM UTC+5, PhistucK wrote:
I think WebView can inject scripts to pages.
So I suggest you try to trace it, from the API to the renderer code that does that, using cs.chromium.org.



Well, yes. I could see the sources starting 'webView'.evaluateJavaScript(..) too.
I was just curious if someone can suggest a shorter way.
 
PhistucK



PhistucK

Андрей Кадацкий

unread,
Sep 5, 2017, 11:11:43 AM9/5/17
to Chromium-dev
Hi Anton, I did some JS injections onPageLoadStarted. 
In my case case it was implemented like this:

In ChromeActivity or ChromeTabbedActivity you have TabModelSelectorTabObserver with onPageLoadStarted(Tab tab, String url){...} callback. You can get WebContents via tab.getWebContents() from it. On WebContents object you call evaluateJavaScript(String script, JavaScriptCallback callback) method, where script is string with your JS code.

Anton Smirnov

unread,
Sep 7, 2017, 2:12:37 AM9/7/17
to Chromium-dev
Hi, Andrew.


On Tuesday, September 5, 2017 at 8:11:43 PM UTC+5, Андрей Кадацкий wrote:
Hi Anton, I did some JS injections onPageLoadStarted. 
In my case case it was implemented like this:

In ChromeActivity or ChromeTabbedActivity you have TabModelSelectorTabObserver with onPageLoadStarted(Tab tab, String url){...} callback. You can get WebContents via tab.getWebContents() from it. On WebContents object you call evaluateJavaScript(String script, JavaScriptCallback callback) method, where script is string with your JS code.

Thanks for pointing the direction! What about C++ side, where an invocation to `TabModelSelectorTabObserver. onPageLoadStarted` comes from? Does it come from .RenderViewHost::OnMsgNavigate  originally?

Андрей Кадацкий

unread,
Sep 7, 2017, 4:37:06 AM9/7/17
to Chromium-dev
But why do you need to know where it invocation goes from? 

Anton Smirnov

unread,
Sep 8, 2017, 1:42:15 AM9/8/17
to Chromium-dev
I've already implemented a part of integration in C++ code (chrome_network_delegate.cc) to filter out undesired resources.
So it be be consistent to introduce changes in C++ code rather than in Java/Android.

Anton Smirnov

unread,
Oct 16, 2017, 7:07:48 AM10/16/17
to Chromium-dev
Does anyone know how to implement it in C++?
It seems that one can invoke javascript with frame's `ExecuteJavaScript(..)`.
Any suggestion on how to properly catch 'page load started' event?

Alex Clarke

unread,
Oct 16, 2017, 7:19:30 AM10/16/17
to d...@antonsmirnov.name, Chromium-dev
Are you aware of DevTools Network.setRequestInterceptionEnabled?  That can filter resources. See https://chromedevtools.github.io/devtools-protocol/tot/Network/#method-setRequestInterceptionEnabled

There are C++ bindings for that in headless_lib, but it's mostly used via node.js

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev+unsubscribe@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/dbe8f58e-f2fb-4f44-9dcf-1d991b56ddcf%40chromium.org.

SPEC INDIA DEV

unread,
Aug 29, 2018, 3:13:51 PM8/29/18
to Chromium-dev
Thanks Andrew, for much needed idea.

It worked for me but with evaluateJavaScriptForTest and not evaluateJavascript method. This solution seems to be patch. Is there any other way to make it work with evaluateJavascript.

Thanks Again
Reply all
Reply to author
Forward
0 new messages