提取 Chrome 扩展的相关信息

问题描述 投票:0回答:0

我正在尝试构建一个 chrome 扩展程序,当用户访问一个网站时,它会聚合来自一堆网站的信息 A


async function fetchHTML(url) {
    const response = await fetch(proxyUrl + url);
    const html = await response.text();
    console.log(html);
    return html;
  }

  // Function to extract the element - total violations from the HTML content
  function extractTotalViolations(html) {
    const parser = new DOMParser();
    const doc = parser.parseFromString(html, "text/html");
    const totalViolations = doc.querySelector(".total-violations").textContent;
    return totalViolations;
  }
  
  // The URL of the page we want to scrape
  const url = "https://example_of_a_url.com";
  
  // Fetch the HTML content of the page and extract the total violations
  fetchHTML(url).then(html => {
    const totalViolations = extractTotalViolations(html);
    console.log(totalViolations);
  });

当我打印 totalViolations 时,我得到 NULL。所以我打印了获取的 HTML 并且我意识到我得到了一些 javascript 代码,这些代码看起来不像我直接在网站上看到的 HTMl 代码。我怀疑该网站使用了一些 javascript 掩码,或者我没有正确获取 HTML

<script>
!function(e){function t(t){for(var n,l,i=t[0],f=t[1],a=t[2],p=0,s=[];p<i.length;p++)l=i[p],Object.prototype.hasOwnProperty.call(o,l)&&o[l]&&s.push(o[l][0]),o[l]=0;for(n in f)Object.prototype.hasOwnProperty.call(f,n)&&(e[n]=f[n]);for(c&&c(t);s.length;)s.shift()();return u.push.apply(u,a||[]),r()}function r(){for(var e,t=0;t<u.length;t++){for(var r=u[t],n=!0,i=1;i<r.length;i++){var f=r[i];0!==o[f]&&(n=!1)}n&&(u.splice(t--,1),e=l(l.s=r[0]))}return e}var n={},o={1:0},u=[];function l(t){if(n[t])return n[t].exports;var r=n[t]={i:t,l:!1,exports:{}};return e[t].call(r.exports,r,r.exports,l),r.l=!0,r.exports}l.m=e,l.c=n,l.d=function(e,t,r){l.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},l.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",
</script>

我的问题是如何正确提取 HTMl,以便我可以解析 DOM 并从该站点获取我想放在扩展中的所有信息。谢谢

javascript google-chrome google-chrome-extension domparser
© www.soinside.com 2019 - 2024. All rights reserved.