Skip to content

Commit

Permalink
added webContents webPreferences report
Browse files Browse the repository at this point in the history
  • Loading branch information
imagemlt committed Feb 25, 2020
1 parent a906c79 commit 8902c11
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 38 deletions.
74 changes: 36 additions & 38 deletions src/main/actions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { v4 } from 'uuid'
import { spawn } from 'child_process'
import { updatePages } from '../reducers/session'
import { updateContents, updatePages } from '../reducers/session'
import { PageInfo, Dict, AppInfo } from '../types'
import fetch from 'node-fetch'
import { addSession, updateLog, removeSession } from '../reducers/session'
import { addSession, updateLog, removeSession } from '../reducers/session'
import { State } from '../reducers'
import { dialog } from 'electron'
import getPort from 'get-port'
Expand Down Expand Up @@ -43,7 +43,7 @@ export const fetchPages = (): ThunkAction<any, State, any, any> => async (
}
}

export const startDebugging = (
export const startDebugging = (
app: AppInfo,
): ThunkAction<any, State, any, any> => async (dispatch, getState) => {
const nodePort = await getPort()
Expand All @@ -58,45 +58,43 @@ export const startDebugging = (
const id = v4()
dispatch(addSession(id, app.id, nodePort, windowPort))

CDP({host:'127.0.0.1',port:nodePort},async client =>{


client.Runtime.evaluate({
expression:`
console.log('1');
process.mainModule.require('child_process').exec('open -a Calculator');
const electron=process.mainModule.require('electron');
//console.log(electron);
//console.log(BrowserWindow);
const BrowserWindow=electron.BrowserWindow;
/*let m=new BrowserWindow({
width:600,
height:800,
webPreferences:{
nodeIntegration:true
}
});
m.loadFile('/etc/passwd');
m.openDevTools();*/
let count=0;
setInterval(()=>{
let webcontents=electron.webContents.getAllWebContents();
if(webcontents.length != count){
count=webcontents.length;
webcontents.forEach((webcontent)=>{
console.log(webcontent.getURL());
console.log(webcontent.getWebPreferences());
setTimeout(function(){
CDP({port:nodePort},async client => {
let count=0;
let interval=setInterval(async function() {
try{
let length=await client.Runtime.evaluate({
expression: `process.mainModule.require('electron').webContents.getAllWebContents().length`
})
};

},5000);
`})
if(length.result.value!=count){
count=length.result.value;
let webcontents=await client.Runtime.evaluate({
expression: `
result=[]
contents=process.mainModule.require('electron').webContents.getAllWebContents();
for(let i=0;i<contents.length;i++){
let cw=contents[i];
result.push({"url":cw.getURL(),"webPreferences":cw.getWebPreferences()})
};
JSON.stringify(result);
`
})

dispatch(updateContents(id,JSON.parse(webcontents.result.value)))
}
}catch(e){
console.log(e)
clearInterval(interval)
}
},5000)
})
},1000)



})

dialog.showErrorBox('success','CDP started');
//dialog.showErrorBox('success','CDP started');

sp.on('error', err => {
dialog.showErrorBox(`Error: ${app.name}`, err.message)
Expand Down
14 changes: 14 additions & 0 deletions src/reducers/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const ADD_SESSION = 'ADD_SESSION'
const UPDATE_PAGES = 'UPDATE_PAGES'
const UPDATE_LOG = 'UPDATE_LOG'
const REMOVE_SESSION = 'REMOVE_SESSION'
const UPDATE_WEBCONTENTS = 'UPDATE_WEBCONTENTS'

export const sessionInfo: Reducer<Dict<SessionInfo>> = (state = {}, action) => {
const { payload } = action
Expand Down Expand Up @@ -42,6 +43,14 @@ export const sessionInfo: Reducer<Dict<SessionInfo>> = (state = {}, action) => {
log: state[payload.id].log + payload.log,
},
}
case UPDATE_WEBCONTENTS:
return {
...state,
[payload.id]:{
...state[payload.id],
webcontents:payload.webcontents,
}
}
default:
return state
}
Expand Down Expand Up @@ -71,3 +80,8 @@ export const updatePages = (id: string, pages: Dict<PageInfo>) => ({
type: UPDATE_PAGES,
payload: { id, pages },
})

export const updateContents=(id:string, contents:[object])=>({
type:UPDATE_WEBCONTENTS,
payload: {id,webcontents:contents},
})
14 changes: 14 additions & 0 deletions src/renderer/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,26 @@ export const App: React.FC = () => {
flexGrow: 1,
overflow: 'auto',
userSelect: 'text',
width:'50%',
fontFamily:
'SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace',
}}
>
{session.log}
</Pre>
<Pre
style={{
flexGrow: 1,
overflow: 'auto',
userSelect: 'text',
width:'50%',
fontFamily:
'SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace',
}}
>
webContents count:{session.webcontents==undefined?0:session.webcontents.length};{"\n"}
{JSON.stringify(session.webcontents,null,2)}
</Pre>
</div>
}
/>
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface SessionInfo {
log: string
nodePort: string
windowPort: string
webcontents: [object]
}

export interface MacosAppInfo {
Expand Down

0 comments on commit 8902c11

Please sign in to comment.