Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Listening to the beforeload event #13140

Closed
fb55 opened this issue Apr 9, 2015 · 9 comments
Closed

Listening to the beforeload event #13140

fb55 opened this issue Apr 9, 2015 · 9 comments
Labels

Comments

@fb55
Copy link

fb55 commented Apr 9, 2015

Not sure if this is a bug, a feature request or just a question.

I'm trying to identify ads on pages loaded by phantom. In Safari, this is doable using the beforeload event, which needs to be injected before the page starts rendering. The event is still present in qtwebkit.

I tried to listen to the event at several points in the page's life cycle, don't seem to be able to receive any notifications, though. I would appreciate any help to get this working.

@iradul
Copy link
Contributor

iradul commented Apr 10, 2015

Have you looked at documentation? You can examine and abort any unwanted resource request using onResourceRequested callback http://phantomjs.org/api/webpage/handler/on-resource-requested.html.

@fb55
Copy link
Author

fb55 commented Apr 10, 2015

Yes, I saw onResourceRequested, but it lacks in two ways: First of all, the type of content that's requested isn't visible, so there is no way to distinguish between requests from eg. included resources or XHRs. Secondly, I want to identify the section of a page consisting of ads, without removing the content (for a thesis – there is probably no way this is useful for everyday users). The beforeload event would do exactly what I need.

There doesn't seem to be a way to communicate with the phantom context in a synchronous way (as provided in Safari by safari.self.tab.canLoad), but for my use-case at least, browserify will do the job.

@zackw
Copy link
Collaborator

zackw commented Apr 24, 2015

Are you testing version 2.0? The edition of Webkit in 1.x is very old and buggy.

This is probably a matter of needing to hook some events up properly, and we'd be happy to work with you to make that happen, but we're not developing 1.x anymore and we have limited time for development so we might need you to take the lead on coding up the necessary changes. Sorry.

@fb55
Copy link
Author

fb55 commented Apr 24, 2015

$ phantomjs -v
2.0.0

My current solution is to preprocess all loaded pages to add a script tag as the first child of the head, which listens to the event and then removes itself immediately. Quite cumbersome, appears to be working, though. It would be great if phantom could do this for me & I'd be happy to help with the implementation.

@zackw zackw removed the Test 2.0 label Apr 24, 2015
@zackw
Copy link
Collaborator

zackw commented Apr 24, 2015

OK. I think the way we would expose beforeload in PhantomJS is as a WebPage.onBeforeLoad callback function. Look at how the existing onXXX callbacks are implemented to see how to do that. The relevant code is in webpage.cpp, I think.

@fb55
Copy link
Author

fb55 commented Apr 24, 2015

The primary uses for beforeload would be the .target property and the .preventDefault method, both presumably not thread-safe. A possibility to inject scripts into pages before the page is loaded would probably be of more use, although I currently don't understand page life cycles well enough to implement something.

@JamesMGreene
Copy link
Collaborator

If a page already has a script tag on it, then you can leverage the WebPage#onInitialized callback, which is executed immediately before the first script is evaluated.

http://phantomjs.org/api/webpage/handler/on-initialized.html

@fb55
Copy link
Author

fb55 commented Apr 25, 2015

That's how that event works? The documentation suggests it fires at the point where Safari would inject start scripts, when the page was created, but before the HTTP request for the page itself was made (which is what I'm looking for). The current implementation doesn't help, as the first request isn't necessarily a script. Manually inserting a script, as I'm doing it right now, would of course be a solution, although the status quo wouldn't be improved.

Phantom seems also to be lacking the ability to inject scripts into frames (eg. iframes), which should probably also be covered by a solution.

@ghost ghost removed Improvement labels Jan 10, 2018
@stale stale bot added the stale label Dec 25, 2019
@stale
Copy link

stale bot commented Dec 28, 2019

Due to our very limited maintenance capacity (see #14541 for more details), we need to prioritize our development focus on other tasks. Therefore, this issue will be automatically closed. In the future, if we see the need to attend to this issue again, then it will be reopened. Thank you for your contribution!

@stale stale bot closed this as completed Dec 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants