mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
初步实现js录屏功能
This commit is contained in:
parent
605fd6b30e
commit
3b6b25b935
@ -631,16 +631,13 @@ class ChromiumBase(BasePage):
|
|||||||
:param item: 要获取的项,不设置则返回全部
|
:param item: 要获取的项,不设置则返回全部
|
||||||
:return: sessionStorage一个或所有项内容
|
:return: sessionStorage一个或所有项内容
|
||||||
"""
|
"""
|
||||||
# js = f'sessionStorage.getItem("{item}");' if item else 'sessionStorage;'
|
|
||||||
# return self.run_js_loaded(js, as_expr=True)
|
|
||||||
|
|
||||||
if item:
|
if item:
|
||||||
js = f'sessionStorage.getItem("{item}");'
|
js = f'sessionStorage.getItem("{item}");'
|
||||||
return self.run_js_loaded(js, as_expr=True)
|
return self.run_js_loaded(js, as_expr=True)
|
||||||
else:
|
else:
|
||||||
js = '''
|
js = '''
|
||||||
var dp_ls_len = sessionStorage.length; // 获取长度
|
var dp_ls_len = sessionStorage.length;
|
||||||
var dp_ls_arr = new Array(); // 定义数据集
|
var dp_ls_arr = new Array();
|
||||||
for(var i = 0; i < dp_ls_len; i++) {
|
for(var i = 0; i < dp_ls_len; i++) {
|
||||||
var getKey = sessionStorage.key(i);
|
var getKey = sessionStorage.key(i);
|
||||||
var getVal = sessionStorage.getItem(getKey);
|
var getVal = sessionStorage.getItem(getKey);
|
||||||
@ -660,8 +657,8 @@ class ChromiumBase(BasePage):
|
|||||||
return self.run_js_loaded(js, as_expr=True)
|
return self.run_js_loaded(js, as_expr=True)
|
||||||
else:
|
else:
|
||||||
js = '''
|
js = '''
|
||||||
var dp_ls_len = localStorage.length; // 获取长度
|
var dp_ls_len = localStorage.length;
|
||||||
var dp_ls_arr = new Array(); // 定义数据集
|
var dp_ls_arr = new Array();
|
||||||
for(var i = 0; i < dp_ls_len; i++) {
|
for(var i = 0; i < dp_ls_len; i++) {
|
||||||
var getKey = localStorage.key(i);
|
var getKey = localStorage.key(i);
|
||||||
var getVal = localStorage.getItem(getKey);
|
var getVal = localStorage.getItem(getKey);
|
||||||
@ -1202,11 +1199,11 @@ class Screencast(object):
|
|||||||
|
|
||||||
else:
|
else:
|
||||||
if self._cid is None:
|
if self._cid is None:
|
||||||
self._cid = self._page.run_cdp('Page.createIsolatedWorld', frameId=self._page.tab_id)[
|
self._cid = self._page.run_cdp('Page.createIsolatedWorld',
|
||||||
'executionContextId']
|
frameId=self._page.tab_id)['executionContextId']
|
||||||
js = '''
|
js = '''
|
||||||
function () {
|
async function () {
|
||||||
stream = navigator.mediaDevices.getDisplayMedia({video: true, audio: true})
|
stream = await navigator.mediaDevices.getDisplayMedia({video: true, audio: true})
|
||||||
mime = MediaRecorder.isTypeSupported("video/webm; codecs=vp9")
|
mime = MediaRecorder.isTypeSupported("video/webm; codecs=vp9")
|
||||||
? "video/webm; codecs=vp9"
|
? "video/webm; codecs=vp9"
|
||||||
: "video/webm"
|
: "video/webm"
|
||||||
@ -1214,11 +1211,16 @@ class Screencast(object):
|
|||||||
chunks = []
|
chunks = []
|
||||||
mediaRecorder.addEventListener('dataavailable', function(e) {chunks.push(e.data)})
|
mediaRecorder.addEventListener('dataavailable', function(e) {chunks.push(e.data)})
|
||||||
mediaRecorder.start()
|
mediaRecorder.start()
|
||||||
|
|
||||||
|
mediaRecorder.addEventListener('stop', function(){
|
||||||
|
blob = new Blob(chunks, {type: chunks[0].type});
|
||||||
|
blob_ok=true;
|
||||||
|
})
|
||||||
}
|
}
|
||||||
'''
|
'''
|
||||||
print('请手动选择要录制的目标。')
|
print('请手动选择要录制的目标。')
|
||||||
r = self._page.run_cdp('Runtime.callFunctionOn', functionDeclaration=js, executionContextId=self._cid)
|
self._page.run_js('var blob;var blob_ok=false;')
|
||||||
print(r)
|
self._page.run_js(js)
|
||||||
|
|
||||||
def stop(self, video_name=None):
|
def stop(self, video_name=None):
|
||||||
"""停止录屏
|
"""停止录屏
|
||||||
@ -1231,13 +1233,12 @@ class Screencast(object):
|
|||||||
path = f'{self._path}{sep}{name}'
|
path = f'{self._path}{sep}{name}'
|
||||||
|
|
||||||
if self._mode.startswith('js'):
|
if self._mode.startswith('js'):
|
||||||
js = '''
|
self._page.run_js('mediaRecorder.stop();', as_expr=True)
|
||||||
mediaRecorder.stop()
|
while not self._page.run_js('return blob_ok'):
|
||||||
return new Blob(chunks, {type: chunks[0].type})
|
sleep(.1)
|
||||||
'''
|
blob = self._page.run_js('return blob;')
|
||||||
bid = self._page.run_cdp('Runtime.callFunctionOn', functionDeclaration=js, executionContextId=self._cid)
|
uuid = self._page.run_cdp('IO.resolveBlob', objectId=blob['result']['objectId'])['uuid']
|
||||||
uuid = self._page.run_cdp('IO.resolveBlob', objectId=bid['objectId'])
|
data = self._page.run_cdp('IO.read', handle=f'blob:{uuid}')['data']
|
||||||
data = self._page.run_cdp('IO.read', handle=f'blob://{uuid}')
|
|
||||||
with open(path, 'wb') as f:
|
with open(path, 'wb') as f:
|
||||||
f.write(b64decode(data))
|
f.write(b64decode(data))
|
||||||
return path
|
return path
|
||||||
|
@ -1387,7 +1387,7 @@ def parse_js_result(page, ele, result):
|
|||||||
ownProperties=True)['result']
|
ownProperties=True)['result']
|
||||||
return [parse_js_result(page, ele, result=i['value']) for i in r[:-1]]
|
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'],
|
r = page.run_cdp('Runtime.getProperties', objectId=result['objectId'],
|
||||||
ownProperties=True)['result']
|
ownProperties=True)['result']
|
||||||
return {i['name']: parse_js_result(page, ele, result=i['value']) for i in r}
|
return {i['name']: parse_js_result(page, ele, result=i['value']) for i in r}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user