Javascript 如何将DOM元素从页面发送到chrome extension popup.html?

Javascript 如何将DOM元素从页面发送到chrome extension popup.html?,javascript,google-chrome-extension,Javascript,Google Chrome Extension,我需要从Instagram获取评论,并将其粘贴到带有发件人链接和评论文本的扩展popup.html中。如何从Instagram页面获取评论元素并使用js将其发送到扩展弹出窗口 popup.js function injectTheScript() { chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { chrome.tabs.executeScript(tabs[0].id, {f

我需要从Instagram获取评论,并将其粘贴到带有发件人链接和评论文本的扩展popup.html中。如何从Instagram页面获取评论元素并使用js将其发送到扩展弹出窗口

popup.js

function injectTheScript() {
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        chrome.tabs.executeScript(tabs[0].id, {file: "content_script.js"});

    });
}

document.getElementById('loadcomments').addEventListener('click', injectTheScript);
function clickLoadMoreComments() {
    var loadMoreCommentsButton = document.getElementsByClassName("_m3m1c _1s3cd")[0];
    var timerId = setTimeout(function clickAgain() {
        if (document.body.contains(loadMoreCommentsButton)){
        loadMoreCommentsButton.click();
        }
    timerId = setTimeout(clickAgain, 1000);
    if (!document.body.contains(loadMoreCommentsButton)){
        clearTimeout(timerId);
    }
  });
}

function randomInteger(min, max) {
    var rand = min - 0.5 + Math.random() * (max - min + 1)
    rand = Math.round(rand);
    return rand;
}

function getRandomComment(){
    var comments = document.getElementsByClassName("_ezgzd");
    var commentsCount = comments.length - 1;
    var randomCommentIndex = randomInteger(1, commentsCount);
    return comments[randomCommentIndex];
}
clickLoadMoreComments();
var port = chrome.runtime.connect();
port.postMessage(
                {
                    commentsCount: (comments.length-1).toString()+" comments founded!",
                    message : "finished",
                    comments : JSON.stringify(Array.from(comments).map(a => a.innerHTML), null, '\t')
                }
            );
chrome.runtime.onConnect.addListener(function(port) {
    port.onMessage.addListener(function(msg) {
            var innerHTMLs = JSON.parse(msg.comments);
        }
    });
  });

content\u script.js

function injectTheScript() {
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        chrome.tabs.executeScript(tabs[0].id, {file: "content_script.js"});

    });
}

document.getElementById('loadcomments').addEventListener('click', injectTheScript);
function clickLoadMoreComments() {
    var loadMoreCommentsButton = document.getElementsByClassName("_m3m1c _1s3cd")[0];
    var timerId = setTimeout(function clickAgain() {
        if (document.body.contains(loadMoreCommentsButton)){
        loadMoreCommentsButton.click();
        }
    timerId = setTimeout(clickAgain, 1000);
    if (!document.body.contains(loadMoreCommentsButton)){
        clearTimeout(timerId);
    }
  });
}

function randomInteger(min, max) {
    var rand = min - 0.5 + Math.random() * (max - min + 1)
    rand = Math.round(rand);
    return rand;
}

function getRandomComment(){
    var comments = document.getElementsByClassName("_ezgzd");
    var commentsCount = comments.length - 1;
    var randomCommentIndex = randomInteger(1, commentsCount);
    return comments[randomCommentIndex];
}
clickLoadMoreComments();
var port = chrome.runtime.connect();
port.postMessage(
                {
                    commentsCount: (comments.length-1).toString()+" comments founded!",
                    message : "finished",
                    comments : JSON.stringify(Array.from(comments).map(a => a.innerHTML), null, '\t')
                }
            );
chrome.runtime.onConnect.addListener(function(port) {
    port.onMessage.addListener(function(msg) {
            var innerHTMLs = JSON.parse(msg.comments);
        }
    });
  });

content\u script.js

function injectTheScript() {
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        chrome.tabs.executeScript(tabs[0].id, {file: "content_script.js"});

    });
}

document.getElementById('loadcomments').addEventListener('click', injectTheScript);
function clickLoadMoreComments() {
    var loadMoreCommentsButton = document.getElementsByClassName("_m3m1c _1s3cd")[0];
    var timerId = setTimeout(function clickAgain() {
        if (document.body.contains(loadMoreCommentsButton)){
        loadMoreCommentsButton.click();
        }
    timerId = setTimeout(clickAgain, 1000);
    if (!document.body.contains(loadMoreCommentsButton)){
        clearTimeout(timerId);
    }
  });
}

function randomInteger(min, max) {
    var rand = min - 0.5 + Math.random() * (max - min + 1)
    rand = Math.round(rand);
    return rand;
}

function getRandomComment(){
    var comments = document.getElementsByClassName("_ezgzd");
    var commentsCount = comments.length - 1;
    var randomCommentIndex = randomInteger(1, commentsCount);
    return comments[randomCommentIndex];
}
clickLoadMoreComments();
var port = chrome.runtime.connect();
port.postMessage(
                {
                    commentsCount: (comments.length-1).toString()+" comments founded!",
                    message : "finished",
                    comments : JSON.stringify(Array.from(comments).map(a => a.innerHTML), null, '\t')
                }
            );
chrome.runtime.onConnect.addListener(function(port) {
    port.onMessage.addListener(function(msg) {
            var innerHTMLs = JSON.parse(msg.comments);
        }
    });
  });
popup.js

function injectTheScript() {
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        chrome.tabs.executeScript(tabs[0].id, {file: "content_script.js"});

    });
}

document.getElementById('loadcomments').addEventListener('click', injectTheScript);
function clickLoadMoreComments() {
    var loadMoreCommentsButton = document.getElementsByClassName("_m3m1c _1s3cd")[0];
    var timerId = setTimeout(function clickAgain() {
        if (document.body.contains(loadMoreCommentsButton)){
        loadMoreCommentsButton.click();
        }
    timerId = setTimeout(clickAgain, 1000);
    if (!document.body.contains(loadMoreCommentsButton)){
        clearTimeout(timerId);
    }
  });
}

function randomInteger(min, max) {
    var rand = min - 0.5 + Math.random() * (max - min + 1)
    rand = Math.round(rand);
    return rand;
}

function getRandomComment(){
    var comments = document.getElementsByClassName("_ezgzd");
    var commentsCount = comments.length - 1;
    var randomCommentIndex = randomInteger(1, commentsCount);
    return comments[randomCommentIndex];
}
clickLoadMoreComments();
var port = chrome.runtime.connect();
port.postMessage(
                {
                    commentsCount: (comments.length-1).toString()+" comments founded!",
                    message : "finished",
                    comments : JSON.stringify(Array.from(comments).map(a => a.innerHTML), null, '\t')
                }
            );
chrome.runtime.onConnect.addListener(function(port) {
    port.onMessage.addListener(function(msg) {
            var innerHTMLs = JSON.parse(msg.comments);
        }
    });
  });

欢迎来到SO。请访问,了解如何提问。提示:发布工作和代码-首先搜索bookmarklet您不能发送DOM元素。使用内容脚本并从元素的属性发送文本。