从wegpage捕获文本/单词并使用JavaScript将其导入到csv文件

oaxa6hgo  于 2023-11-14  发布在  Java
关注(0)|答案(1)|浏览(104)

这段代码工作得很好,按钮,导入数据到csv文件,但const文本和错误处理是不工作.基本上我需要的是从网页捕捉单词,例如从这段代码,我想捕捉单词“谷歌”.它的工作,但如果我把其他的话,是不是在网页中,错误处理是不工作,它的捕捉任何字,我把const文本.
此外,我想捕捉的话,有没有一种方法可以分开它与不同的标题每列?
我想要一个像下面这样的结果:

Word1    Word2           Word3
Google   Google Search   I'm Feeling Lucky

字符串

// ==UserScript==
// @name         Google Text to CSV
// @namespace    http://your-website.com
// @version      1.0
// @description  Capture words from Google's webpage and export it to a CSV file
// @author       Your Name
// @match        https://www.google.com/*
// @grant        GM_download
// ==/UserScript==

(function() {
    'use strict';

    // Function to capture the words from the webpage
    function captureTextFromGoogle() {
        const text = ("Google", "Google Search", "I'm Feeling Lucky");

        return text;

    }

    // Function to export the captured text to a CSV file
    function exportToCSV(text) {
        // Create a CSV content
        const csvContent = `"Word","Word2","Word3"\n"${text}"`;
        console.log(csvContent);
        // Create a Blob containing the CSV data
        const blob = new Blob([csvContent], { type: 'text/csv' });

        // Create a download link and trigger the download
        const a = document.createElement('a');
        a.href = URL.createObjectURL(blob);
        a.download = 'captured_text.csv';
        a.style.display = 'none';
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
    }

    // Create a button to trigger text capture and export
    const captureButton = document.createElement('button');
    captureButton.textContent = 'Export Data to CSV';
//    captureButton.style.position = 'fixed';
//    captureButton.style.top = '350px';
//    captureButton.style.right = '230px';
    captureButton.style.zIndex = '9999';
    captureButton.style.padding = '10px';
    captureButton.style.border = '1px solid #000';
    captureButton.style.backgroundColor = '#0077b6';
    captureButton.style.color = '#fff';
    captureButton.style.cursor = 'pointer';
    document.body.appendChild(captureButton);

    captureButton.addEventListener('click', () => {
        try {
            // Capture the text from the webpage
            const capturedText = captureTextFromGoogle();

            // Export the captured text to a CSV file
            exportToCSV(capturedText);
        } catch (error) {
            console.error('Error:', error);
        }
    });
})();

0s0u357o

0s0u357o1#

你需要一个数组,你需要引用内容,但你需要删除文字中的引号:

function captureTextFromGoogle() {
    const list = ["Google", "Google Search", "I'm Feeling Lucky"];
    return list.map(word => `"${word}"`).join(",");
}

// Function to export the captured text to a CSV file
function exportToCSV(text) {
    // Create a CSV content
    const csvContent = `"Word","Word2","Word3"\n${text}`;

字符串

// ==UserScript==
// @name         Google Text to CSV
// @namespace    http://your-website.com
// @version      1.0
// @description  Capture words from Google's webpage and export it to a CSV file
// @author       Your Name
// @match        https://www.google.com/*
// @grant        GM_download
// ==/UserScript==

(function() {
    'use strict';

    // Function to capture the words from the webpage
    function captureTextFromGoogle() {
        const list = ["Google", "Google Search", "I'm Feeling Lucky"];
        return list.map(word => `"${word}"`).join(",");
    }

    // Function to export the captured text to a CSV file
    function exportToCSV(text) {
        // Create a CSV content
        const csvContent = `"Word","Word2","Word3"\n${text}`;
        console.log(csvContent);
        // Create a Blob containing the CSV data
        const blob = new Blob([csvContent], { type: 'text/csv' });

        // Create a download link and trigger the download
        const a = document.createElement('a');
        a.href = URL.createObjectURL(blob);
        a.download = 'captured_text.csv';
        a.style.display = 'none';
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
    }

    // Create a button to trigger text capture and export
    const captureButton = document.createElement('button');
    captureButton.textContent = 'Export Data to CSV';
//    captureButton.style.position = 'fixed';
//    captureButton.style.top = '350px';
//    captureButton.style.right = '230px';
    captureButton.style.zIndex = '9999';
    captureButton.style.padding = '10px';
    captureButton.style.border = '1px solid #000';
    captureButton.style.backgroundColor = '#0077b6';
    captureButton.style.color = '#fff';
    captureButton.style.cursor = 'pointer';
    document.body.appendChild(captureButton);

    captureButton.addEventListener('click', () => {
        try {
            // Capture the text from the webpage
            const capturedText = captureTextFromGoogle();

            // Export the captured text to a CSV file
            exportToCSV(capturedText);
        } catch (error) {
            console.error('Error:', error);
        }
    });
})();

相关问题