Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.3 kB
1
Indexable
Never
/*
To use this extension, you would first load the manifest.json and background.js files as an unpacked extension in Chrome. Then, when you click the extension's browser action button, it will scrape the current page and log the data to the console. You can modify the scrapePage function in content.js to extract the specific data that you are interested in from the page.
*/



// content.js

// listen for messages from the background script
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
    // if the message is to scrape the current page
    if (message.action == "scrape") {
        // scrape the page and send the data back to the background script
        var data = scrapePage();
        sendResponse({data: data});
    }
});

// scrape the page
function scrapePage() {
    // get the title and all paragraph elements on the page
    var title = document.title;
    var paragraphs = document.querySelectorAll("p");

    // create an array of paragraph text
    var data = [];
    for (var i = 0; i < paragraphs.length; i++) {
        data.push(paragraphs[i].innerText);
    }

    // return the title and paragraph data
    return {
        title: title,
        data: data
    }
}