You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

79 lines
2.1 KiB

import asyncio
import time
from pathlib import Path
import pyppeteer
EVENT_LOOP = None
def run(coroutine):
global EVENT_LOOP
if EVENT_LOOP is None:
EVENT_LOOP = asyncio.get_event_loop()
return EVENT_LOOP.run_until_complete(coroutine)
class BrowserConnection:
def __init__(self, address=None, browser_handle: pyppeteer.browser.Browser = None):
if browser_handle is None:
self.browser_handle: pyppeteer.browser.Browser = run(
pyppeteer.launcher.connect(browserWSEndpoint=address)
)
elif address is None:
self.browser_handle = browser_handle
self.address = self.browser_handle.wsEndpoint
@property
def tabs(self):
return run(self.browser_handle.pages())
def create_tab(self):
return Tab(self.browser_handle)
def close(self):
run(self.browser_handle.close())
class Tab:
def __init__(self, browser_handle):
self.browser_handle = browser_handle
self.page_handle = run(self.browser_handle.newPage())
def __enter__(self):
return self
def __exit__(self, *args):
run(self.page_handle.close())
def get_source(self):
return run(self.page_handle.content())
def open(self, url, wait_for=0):
run(self.page_handle.goto(url, waitUntil="domcontentloaded"))
time.sleep(wait_for)
return self.get_source()
def start_browser(proxy=None, use_adblock=True, **launch_opts):
opts = launch_opts
opts["autoClose"] = False
if use_adblock:
adblock_path = Path(__file__).parent / "uBlock"
opts.setdefault("args", []).extend(
[
"--disable-extensions-except={}".format(adblock_path),
"--load-extension={}".format(adblock_path),
]
)
if proxy is not None:
opts.setdefault("args", []).extend(["--proxy-server=" + proxy])
opts.setdefault("args", []).append("about:blank")
browser_handle = run(pyppeteer.launch(**opts))
return BrowserConnection(browser_handle=browser_handle)
if __name__ == "__main__":
b = start_browser(headless=False)