最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

网站源码admin41浏览0评论

Google Extantions Nodejs 获取信息(读取'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})



}
回答如下:
发布评论

评论列表(0)

  1. 暂无评论