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

[WIP] Service worker support #391

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

johncoates-st
Copy link

Implementation of:

Hey @route, this is a follow up to our conversation as far as service worker support. I based this PR on your comment:

This is a first draft. I could use a bit more guidance, so I'll leave some notes across the changed files.

@@ -71,11 +71,12 @@ def reset
# @return [Cookies]
attr_reader :cookies

def initialize(target_id, browser, proxy: nil)
def initialize(target_id, browser, proxy: nil, type: "page")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want me to use Page or create another class for workers?

@@ -355,13 +356,21 @@ def subscribe
end

def prepare_page
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I keep everything in this method or split out the worker preparation into a different method?

end

def build(**options)
connection(**options)
end

def build_page(**options)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I just alias_method this to build?

def maybe_sleep_if_new_window
# Dirty hack because new window doesn't have events at all
sleep(NEW_WINDOW_WAIT) if window?
end

def connection(**options)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what the proper naming would be for this if it can be both a page and a worker.

@@ -23,12 +24,19 @@ def attached?
end

def page
@page ||= build_page
connection if page?
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially I had this return nil if it was a worker because other parts of the code were causing failures otherwise, but not sure what the impact of this is.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an image I came up with on Figma, no particular attachment to it if you'd like something else. I tried a SVG but it didn't work with the createImageBitmap code.

let canvas = document.getElementById('canvas');
let context = canvas.getContext('2d');
let imageBitmap = event.data.data;
context.drawImage(imageBitmap, 0, 0);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drawn to the canvas so the page isn't blank.

page.go_to("/ferrum/service_worker")

browser.network.wait_for_idle
traffic = browser.targets.values.map { _1.network.traffic }.flatten
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the worker bubble up its traffic to its parent page?

@route
Copy link
Member

route commented Aug 24, 2023

I'm now on the road but I'll check your implementation this weekend, I hope by that time I'll be home.

@johncoates-st
Copy link
Author

Okay, sounds great!

@route
Copy link
Member

route commented Sep 11, 2023

@johncoates-st let me first finish new headless mode for Ferrum, as there are architecture changes for the gem since frames don't work like they were. So we might need some changes here as well.

@johncoates-st
Copy link
Author

@route Okay, sounds good. What kind of timeline are you thinking for that new headless mode getting merged?

@route
Copy link
Member

route commented Sep 14, 2023

@johncoates-st it's merged, so I'm fully yours ;) I'll start playing with it today.

@johncoates-st
Copy link
Author

@route Great! Looking forward to your feedback

@route
Copy link
Member

route commented Sep 16, 2023

I think first of all we should address Target.setAutoAttach and change code to support Runtime.runIfWaitingForDebugger and sessions. Maybe even remove dedicated connection to page/target, but I currently have no idea how sessions work in Chrome with new flatten mode. There's little info about it, so we need to experiment. After that we can intro workers, doing it all at once seems a bit complicated.

@route
Copy link
Member

route commented Jan 6, 2024

The support for flatten mode and auto-attach landed to the main branch. Could you please rebase and now we can continue on service workers. To answer your questions I think it makes more sense not to derive from the Page class.

@route route added the needs feedback Needs feedback label Jan 6, 2024
@johncoates-st
Copy link
Author

@route That's great! Okay sounds good, I'll rebase.

@jpheos
Copy link

jpheos commented Dec 9, 2024

thanks for your work @johncoates-st
Any update of the merge ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs feedback Needs feedback
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants