electron 如何用电子获取innerHTML?

c2e8gylq  于 2023-09-28  发布在  Electron
关注(0)|答案(2)|浏览(176)

我对electron很陌生。我正在用electron为我的学校作业创建一个简单的网络浏览器。

我想获取显示网站的innerHTML信息。但我根本不知道怎么发动...

// Electronの読み込み
var electron = require('electron');
var app = electron.app;
var ipcMain = electron.ipcMain;

var BrowserWindow = electron.BrowserWindow;
const localShortcut = require('electron-localshortcut');
const dialog = electron.dialog;

// mainWindow変数の初期化
var mainWindow = null;

// MacOS(darwin)でない場合にはアプリを終了する
app.on('window-all-closed', function() {
  if(process.platform != 'darwin')
  app.quit();
});

// 画面を表示.index.htmlを読み込む
// Close処理を行う
app.on('ready', function() {
  // 画面表示
  mainWindow = new BrowserWindow({
    width: 1400, 
    height: 800,
    'webPreferences': {
      'nodeIntegration': true,
      'webviewTag': true
    }
  });
  mainWindow.loadURL('file://' + __dirname + '/index.html')
  //mainWindow.webContents.openDevTools();

  mainWindow.webContents.executeJavaScript(`
    document.addEventListener("copy", e => {
      var copied = window.getSelection().toString();
      console.log(copied);
    });
  `);

  mainWindow.on('ready-to-show', function () {
    mainWindow.show();
    mainWindow.focus();
    mainWindow.webContents.executeJavaScript(`
      require('electron').ipcRenderer.send('gpu', document.body.innerHTML);
    `);
  });

  mainWindow.on('closed', function() {
    mainWindow = null;
  });

  localShortcut.register(mainWindow, 'Command+O', () => {
    dialog.showMessageBox("dev", "opened dev tools");
    mainWindow.webContents.openDevTools()
  })

  localShortcut.register(mainWindow, 'Command+Q', () => {
    app.quit()
  })
});
<!DOCTYPE html>
<html lang="ja" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Chocolate</title>
    <link rel="stylesheet" type="text/css" href="index.css">
    <link rel="stylesheet" href="node_modules/electron-tabs/electron-tabs.css">
  </head>
  <body>
    <div class="etabs-tabgroup">
      <div class="etabs-tabs"></div>
      <div class="etabs-buttons"></div>
    </div>
    <div class="etabs-views"></div>
    
    <script>
      const TabGroup = require("electron-tabs");
    
      let tabGroup = new TabGroup({
        newTab: {
          title: 'New Tab'
        }
      });
    
      tabGroup.addTab({
        title: 'Google',
        src: 'http://google.com',
      });
    
      // tabGroup.addTab({
      //   title: "Electron",
      //   src: "http://electron.atom.io",
      //   visible: true,
      //   active: true
      // });
    
    </script>
  </body>
</html>

你能给予我一些想法或提示吗?

xwmevbvl

xwmevbvl1#

mainWindow.webContents.executeJavaScript(`function gethtml () {
    return new Promise((resolve, reject) => { resolve(document.documentElement.innerHTML); });
    }
    gethtml();`).then((html) => {
    console.log(html)
  })

您可以阅读https://en.jeffprod.com/blog/2019/web-scraping-with-electron/

px9o7tmv

px9o7tmv2#

对我来说很好

win.webContents
  .executeJavaScript(`document.documentElement.innerText`, true)
  .then(async function (result) {
    console.log("RESULT: ", result);
   }

相关问题