Greasy Fork

Greasy Fork is available in English.

大众点评评论

get comments of dianping

当前为 2021-11-08 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @grant        GM.xmlHttpRequest
// @name         大众点评评论
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  get comments of dianping
// @author       You
// @match        http://www.dianping.com/shop*
// @match        https://www.dianping.com/shop*
// @icon         https://www.google.com/s2/favicons?domain=dianping.com
// @grant        none
// @require      http://greasyfork.icu/scripts/435146-html2canvas-132/code/html2canvas132.js?version=986217
// @require      https://unpkg.com/[email protected]/dist/tesseract.min.js
// ==/UserScript==
/* global html2canvas Tesseract */

// console.log(GM_info);
const moreBtnClass = '.fold';
const lessBtnClass = '.unfold';
const commentClass = '.review-words';
const ppocrUrl = 'https://www.paddlepaddle.org.cn/paddlehub-api/image_classification/chinese_ocr_db_crnn_mobile';

(function() {
  'use strict';
  const $ = document.querySelectorAll.bind(document);

  const renderCmt = elm => {
    return new Promise((resolve, reject) => {
      html2canvas(elm, {
        allowTaint: true,
        scale: 1,
        useCORS: true
      }).then(canvas => {
          const data = canvas.toDataURL();
          console.log('data', data);
          // document.body.append(canvas);
          GM.xmlHttpRequest({
              method: 'POST',
              url: ppocrUrl,
              onload: response => {
                  console.log(response);
              }
          });

          // return resolve(canvas);
          console.log('start to recognize');
          Tesseract.recognize(canvas, 'chi_sim', {
              langPath: 'https://raw.githubusercontent.com/naptha/tessdata/gh-pages/4.0.0_best/',
          }).then(res => {
              console.log(res);
              const { text } = res.data;
              return resolve(text);
          })
      })
    });
  }

  const getAllCommentCanvas = async () => {
    const comments = $(commentClass);
    let tasks = [];
    for(let i = 0;i <= comments.length; i++) {
        const cmt = comments[i];
        const res = await renderCmt(cmt)
        console.log('res', i, res);
    }
    //comments.forEach((cmt, idx) => {
      //const imgs = cmt.querySelectorAll('img');
      //imgs.forEach(img => cmt.removeChild(img));
        //if (idx === 0) {
            //tasks.push(renderCmt(cmt));
        //}
    //});
    const ret = [];
    return ret;
  }

  let btn = document.createElement('button');
  btn.innerHTML = '开始采集';
  btn.style.position = 'fixed'
  btn.style.right = 0;
  btn.style.bottom = 0;
  document.body.appendChild(btn);
  btn.onclick = () => {
    const moreBtns = $(moreBtnClass);
    moreBtns.forEach(b => {
      b.click();
      b.style.opacity = 0;
    });
    const lessBtns = $(lessBtnClass);
    lessBtns.forEach(l => l.style.opacity = 0);
    getAllCommentCanvas()
      .then(pics => {
        pics.forEach(pic => {
            // console.log('pic', pic);

        })
      })
      .catch(console.error)
  }

})();