Google Extantions Nodejs 获取信息(读取'querySelector')

编程入门 行业动态 更新时间:2024-10-03 23:25:58

Google Extantions Nodejs 获取<a href=https://www.elefans.com/category/jswz/34/1770283.html style=信息(读取'querySelector')"/>

Google Extantions Nodejs 获取信息(读取'querySelector')

我想在我的网站上使用我的谷歌扩展,所以我写了扩展,扩展有按钮,当我按下按钮时,我调用这个函数:

scrapeEmailsFromPage
当我放一些文本时扩展工作。但是我需要在这个 div 类区域中获取邮件地址:

 <div class="ContactView_data__+QQmF"><svg xmlns="" viewBox="0 0 512 512" fill="currentColor" class="Icon_icon__rqEEN ContactView_solidIcon__v5Fuh" role="img" width="16px" height="16px" focusable="false"><title>E-mail address</title>
    <path d="M464 64c26.5 0 48 21.46 48 48 0 15.1-7.1 29.3-19.2 38.4L275.2 313.6a32.1 32.1 0 0 1-37.4 0L19.2 150.4C7.113 141.3 0 127.1 0 112c0-23.51 21.49-48 48-48h416zM217.6 339.2a63.9 63.9 0 0 0 76.8 0L512 176v208c0 33.3-28.7 64-64 64H64c-35.31 0-64-28.7-64-64V176l217.6 163.2z"></path></svg>
<a class="Normalize_normalize__5biS5 Link_root__FUNP5 Link_fontStyling__+NQdw ContactView_contactData__fQo58" href="mailto:[email protected]" rel="noopener noreferrer" role="link" target="_self" title="[email protected]" data-testid="EV_contactperson-email">[email protected]</a></div>

我正在尝试获取此电子邮件地址:[email protected],

我正在尝试这段代码:

function scrapeEmailsFromPage(){
    // const emailRegex = /[\w\.=-]+@[\w\.-]+\.[\w]{2,3}/gim;

    // Parse emails from HTML pf the page
    // let emails = document.body.innerHTML.match(emailRegex)
    // alert(emails)
    const divElement = document.querySelector('div.ContactView_data__+QQmF');
    console.log(divElement)
    const emailElement = divElement.querySelector('a.ContactView_contactData__fQo58');
    const email = emailElement.textContent;
    console.log(email);
    // chrome.runtime.sendMessage({emails})



}

但是在控制台中我得到这个错误:

caught TypeError: Cannot read properties of null (reading 'querySelector')
    at scrapeEmailsFromPage (<anonymous>:8:37)
    at <anonymous>:15:3
s

在这一行

const emailElement = divElement.querySelector('a.ContactView_contactData__fQo58');

我怎样才能得到这个邮件地址?你能帮帮我吗?

所有代码:

let scrapeEmails = document.getElementById('scrapeEmails');

//Handler to receive emails form content script

chrome.runtime.onMessage.addListener((request,sender,sendResponse)=>{
    let emails = request.emails
    alert(emails)
})


scrapeEmails.addEventListener("click", async()=>{

    //Get current active tab
    let [tab] = await chrome.tabs.query({active: true, currentWindow:true});
    // Execute script to parse emails on page
    chrome.scripting.executeScript({
        target: {tabId: tab.id},
        func: scrapeEmailsFromPage,
    })
})

//Function to scrape emails
function scrapeEmailsFromPage(){
    // const emailRegex = /[\w\.=-]+@[\w\.-]+\.[\w]{2,3}/gim;

    // Parse emails from HTML pf the page
    // let emails = document.body.innerHTML.match(emailRegex)
    // alert(emails)
    const divElement = document.querySelector('div.ContactView_data__+QQmF');
    console.log(divElement)
    const emailElement = divElement.querySelector('a.ContactView_contactData__fQo58');
    const email = emailElement.textContent;
    console.log(email);
    // chrome.runtime.sendMessage({emails})



}
回答如下:

更多推荐

Google Extantions Nodejs 获取信息(读取'querySelector')

本文发布于:2024-05-30 11:18:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770440.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:信息   Extantions   Google   Nodejs   querySelector

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!