electron 获取webview里面的元素

<webview id ="web" src ="" allowpopups disablewebsecurity  plugins preload="test.js" ></webview>
test.js:
//目前这个方法可以异步设置定时器一直到网页找你要的元素,但是目前没有找到怎么关闭这个定时器的方法
//比如在webview登录qq,获取qq用户头像,下次再登录判断是否重复登录,如果不关闭程序,当你直接跳转到之前用户页面的时候也会提示你已登录
//除非重新启动electron才能停止找寻页面元素
const ipc = require('electron').ipcRenderer;

var waitForExternal = setInterval(() => {
    var parent = document.getElementById('skin_cover_s');
    //ipc.on('close_interval',()=>{parent = null})
    if(parent!= null)
    {
        //ipc.send('test')
        clearInterval(waitForExternal); 
        waitForExternal=null;
        var image = parent.getElementsByTagName('img');
        var web_src = window.location.href;     
        ipc.send('test',image[0].src,web_src,image[0].alt);

    }
})

猜你喜欢

转载自blog.csdn.net/chenzhigaoa/article/details/80284587