From 3b6b25b93577661483fae857715ac696898dba90 Mon Sep 17 00:00:00 2001 From: g1879 Date: Mon, 20 Mar 2023 19:21:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=9E=E7=8E=B0js=E5=BD=95?= =?UTF-8?q?=E5=B1=8F=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/chromium_base.py | 45 ++++++++++++++++---------------- DrissionPage/chromium_element.py | 2 +- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/DrissionPage/chromium_base.py b/DrissionPage/chromium_base.py index e49a13f..78d9309 100644 --- a/DrissionPage/chromium_base.py +++ b/DrissionPage/chromium_base.py @@ -631,16 +631,13 @@ class ChromiumBase(BasePage): :param item: 要获取的项,不设置则返回全部 :return: sessionStorage一个或所有项内容 """ - # js = f'sessionStorage.getItem("{item}");' if item else 'sessionStorage;' - # return self.run_js_loaded(js, as_expr=True) - if item: js = f'sessionStorage.getItem("{item}");' return self.run_js_loaded(js, as_expr=True) else: js = ''' - var dp_ls_len = sessionStorage.length; // 获取长度 - var dp_ls_arr = new Array(); // 定义数据集 + var dp_ls_len = sessionStorage.length; + var dp_ls_arr = new Array(); for(var i = 0; i < dp_ls_len; i++) { var getKey = sessionStorage.key(i); var getVal = sessionStorage.getItem(getKey); @@ -660,8 +657,8 @@ class ChromiumBase(BasePage): return self.run_js_loaded(js, as_expr=True) else: js = ''' - var dp_ls_len = localStorage.length; // 获取长度 - var dp_ls_arr = new Array(); // 定义数据集 + var dp_ls_len = localStorage.length; + var dp_ls_arr = new Array(); for(var i = 0; i < dp_ls_len; i++) { var getKey = localStorage.key(i); var getVal = localStorage.getItem(getKey); @@ -1202,23 +1199,28 @@ class Screencast(object): else: if self._cid is None: - self._cid = self._page.run_cdp('Page.createIsolatedWorld', frameId=self._page.tab_id)[ - 'executionContextId'] + self._cid = self._page.run_cdp('Page.createIsolatedWorld', + frameId=self._page.tab_id)['executionContextId'] js = ''' - function () { - stream = navigator.mediaDevices.getDisplayMedia({video: true, audio: true}) - mime = MediaRecorder.isTypeSupported("video/webm; codecs=vp9") - ? "video/webm; codecs=vp9" + async function () { + stream = await navigator.mediaDevices.getDisplayMedia({video: true, audio: true}) + mime = MediaRecorder.isTypeSupported("video/webm; codecs=vp9") + ? "video/webm; codecs=vp9" : "video/webm" mediaRecorder = new MediaRecorder(stream, {mimeType: mime}) chunks = [] mediaRecorder.addEventListener('dataavailable', function(e) {chunks.push(e.data)}) mediaRecorder.start() + + mediaRecorder.addEventListener('stop', function(){ + blob = new Blob(chunks, {type: chunks[0].type}); + blob_ok=true; + }) } ''' print('请手动选择要录制的目标。') - r = self._page.run_cdp('Runtime.callFunctionOn', functionDeclaration=js, executionContextId=self._cid) - print(r) + self._page.run_js('var blob;var blob_ok=false;') + self._page.run_js(js) def stop(self, video_name=None): """停止录屏 @@ -1231,13 +1233,12 @@ class Screencast(object): path = f'{self._path}{sep}{name}' if self._mode.startswith('js'): - js = ''' - mediaRecorder.stop() - return new Blob(chunks, {type: chunks[0].type}) - ''' - bid = self._page.run_cdp('Runtime.callFunctionOn', functionDeclaration=js, executionContextId=self._cid) - uuid = self._page.run_cdp('IO.resolveBlob', objectId=bid['objectId']) - data = self._page.run_cdp('IO.read', handle=f'blob://{uuid}') + self._page.run_js('mediaRecorder.stop();', as_expr=True) + while not self._page.run_js('return blob_ok'): + sleep(.1) + blob = self._page.run_js('return blob;') + uuid = self._page.run_cdp('IO.resolveBlob', objectId=blob['result']['objectId'])['uuid'] + data = self._page.run_cdp('IO.read', handle=f'blob:{uuid}')['data'] with open(path, 'wb') as f: f.write(b64decode(data)) return path diff --git a/DrissionPage/chromium_element.py b/DrissionPage/chromium_element.py index 4329d6b..ded40fb 100644 --- a/DrissionPage/chromium_element.py +++ b/DrissionPage/chromium_element.py @@ -1387,7 +1387,7 @@ def parse_js_result(page, ele, result): ownProperties=True)['result'] return [parse_js_result(page, ele, result=i['value']) for i in r[:-1]] - elif 'objectId' in result: # dict + elif 'objectId' in result and result['className'] == 'object': # dict r = page.run_cdp('Runtime.getProperties', objectId=result['objectId'], ownProperties=True)['result'] return {i['name']: parse_js_result(page, ele, result=i['value']) for i in r}