Skip to content

Commit

Permalink
Merge pull request #27 from ClericPy/dev
Browse files Browse the repository at this point in the history
2.2.3
  • Loading branch information
ClericPy authored Jun 15, 2020
2 parents 2572120 + e2fecf5 commit f23621d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 56 deletions.
2 changes: 1 addition & 1 deletion ichrome/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .logs import logger
from .sync_utils import Chrome, Tab

__version__ = "2.2.2"
__version__ = "2.2.3"
__tips__ = "[github]: https://github.com/ClericPy/ichrome\n[cdp]: https://chromedevtools.github.io/devtools-protocol/\n[cmd args]: https://peter.sh/experiments/chromium-command-line-switches/"
__all__ = [
'Chrome', 'ChromeDaemon', 'Tab', 'Tag', 'AsyncChrome', 'AsyncTab', 'logger',
Expand Down
18 changes: 10 additions & 8 deletions ichrome/async_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,8 +674,9 @@ async def wait_event(
if ok:
result = _result
break
except Exception:
continue
except Exception as error:
logger.error(f'wait_event crashed for: {error!r}')
raise error
elif _result:
result = _result
break
Expand Down Expand Up @@ -1190,9 +1191,10 @@ async def querySelectorAll(self,
return TagNotFound()
else:
return result
except Exception as e:
logger.error(f"querySelectorAll error: {e!r}, response: {response}")
return None
except Exception as error:
logger.error(
f"querySelectorAll error: {error!r}, response: {response}")
raise error

async def insertAdjacentHTML(self,
html: str,
Expand Down Expand Up @@ -1818,9 +1820,9 @@ async def get_tabs(self, filt_page_type: bool = True) -> List[Tab]:
for rjson in r.json()
if (rjson["type"] == "page" or filt_page_type is not True)
]
except Exception:
logger.error(
f'fail to get_tabs {self.server}, {traceback.format_exc()}')
except Exception as error:
logger.error(f'fail to get_tabs {self.server}, {error!r}')
raise error
return []

async def get_tab(self, index: int = 0) -> Union[Tab, None]:
Expand Down
106 changes: 59 additions & 47 deletions ichrome/sync_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

from .daemon import ChromeDaemon
from .logs import logger


"""
[Archived]: will not update except bugs.
Sync utils for connections and operations.
Expand Down Expand Up @@ -45,8 +43,9 @@ def _get_tabs(self):
Get all open browser tabs that are pages tabs
"""
try:
r = self.req.get(
self.server + "/json", timeout=self.timeout, retry=self.retry)
r = self.req.get(self.server + "/json",
timeout=self.timeout,
retry=self.retry)
return [
Tab(
tab["id"],
Expand All @@ -56,9 +55,9 @@ def _get_tabs(self):
self,
) for tab in r.json() if tab["type"] == "page"
]
except Exception:
traceback.print_exc()
return []
except Exception as error:
logger.error(f'_get_tabs crashed for: {error!r}')
raise error

@property
def server(self):
Expand Down Expand Up @@ -120,15 +119,15 @@ def close_tabs(self, tab_ids):
return [self.close_tab(tab_id) for tab_id in tab_ids]

def kill(self, timeout=None, max_deaths=1):
ChromeDaemon.clear_chrome_process(
self.port, timeout=timeout, max_deaths=max_deaths)
ChromeDaemon.clear_chrome_process(self.port,
timeout=timeout,
max_deaths=max_deaths)

@property
def meta(self):
r = self.req.get(
f"{self.server}/json/version",
retry=self.retry,
timeout=self.timeout)
r = self.req.get(f"{self.server}/json/version",
retry=self.retry,
timeout=self.timeout)
if r.x and r.ok:
return r.json()

Expand Down Expand Up @@ -248,8 +247,9 @@ def send(self,
with self.lock:
self.ws.send(json.dumps(request))
request = {"id": request["id"]}
res = self.recv(
request, timeout=timeout, callback_function=callback_function)
res = self.recv(request,
timeout=timeout,
callback_function=callback_function)
return res
except (
websocket._exceptions.WebSocketTimeoutException,
Expand Down Expand Up @@ -304,8 +304,9 @@ def get_cookies(self, urls=None, timeout=None):
result = self.send("Network.getCookies", timeout=timeout)
try:
return json.loads(result)["result"]["cookies"]
except Exception:
return []
except Exception as error:
logger.error(f'get_cookies crashed for: {error!r}')
raise error

@property
def current_url(self):
Expand Down Expand Up @@ -334,23 +335,24 @@ def enable(self, name: str):
def disable(self, name: str):
return self.send(f'{name}.disable', timeout=0)

def wait_loading(self, wait_seconds=None, timeout=1,
def wait_loading(self,
wait_seconds=None,
timeout=1,
callback_function=None):
self.enable('Page')
data = self.wait_event(
"Page.loadEventFired",
timeout=timeout,
wait_seconds=wait_seconds,
callback_function=callback_function)
data = self.wait_event("Page.loadEventFired",
timeout=timeout,
wait_seconds=wait_seconds,
callback_function=callback_function)
return data

def wait_event(
self,
event="",
timeout=None,
callback_function=None,
filter_function=None,
wait_seconds=None,
self,
event="",
timeout=None,
callback_function=None,
filter_function=None,
wait_seconds=None,
):
"""ensure enable the method first, or will not listen any event."""
timeout = self.timeout if timeout is None else timeout
Expand Down Expand Up @@ -388,11 +390,10 @@ def set_url(self, url=None, referrer=None, timeout=5):
if referrer is None:
data = self.send("Page.navigate", url=url, timeout=timeout)
else:
data = self.send(
"Page.navigate",
url=url,
referrer=referrer,
timeout=timeout)
data = self.send("Page.navigate",
url=url,
referrer=referrer,
timeout=timeout)
else:
data = self.send("Page.reload", timeout=timeout)
time_passed = self.now - start_load_ts
Expand All @@ -405,8 +406,9 @@ def js(self, javascript, mute_log=False):
"""
Evaluate JavaScript on the page
"""
return self.send(
"Runtime.evaluate", expression=javascript, mute_log=mute_log)
return self.send("Runtime.evaluate",
expression=javascript,
mute_log=mute_log)

def querySelectorAll(self, cssselector, index=None, action=None):
"""
Expand Down Expand Up @@ -477,31 +479,41 @@ def querySelectorAll(self, cssselector, index=None, action=None):
return None
else:
return result
except Exception as e:
logger.info(f"querySelectorAll error: {e}, response: {response}")
if isinstance(index, int):
return None
return []
except Exception as error:
logger.info(
f"querySelectorAll crashed for: {error!r}, response: {response}"
)
raise error

def inject_js_url(self,
url,
timeout=None,
retry=0,
verify=0,
**requests_kwargs):
return self.inject_js(
url, timeout=timeout, retry=retry, verify=verify, **requests_kwargs)

def inject_js(self, url, timeout=None, retry=0, verify=0,
return self.inject_js(url,
timeout=timeout,
retry=retry,
verify=verify,
**requests_kwargs)

def inject_js(self,
url,
timeout=None,
retry=0,
verify=0,
**requests_kwargs):
# js_source_code = """
# var script=document.createElement("script");
# script.type="text/javascript";
# script.src="{}";
# document.getElementsByTagName('head')[0].appendChild(script);
# """.format(url)
r = self.req.get(
url, timeout=timeout, retry=retry, verify=verify, **requests_kwargs)
r = self.req.get(url,
timeout=timeout,
retry=retry,
verify=verify,
**requests_kwargs)
if r.x and r.ok:
javascript = r.text
return self.js(javascript, mute_log=True)
Expand Down

0 comments on commit f23621d

Please sign in to comment.