Export Patent Info

Export patent information to CSV format

// ==UserScript==
// @name         Export Patent Info
// @namespace    http://your.site.com
// @version      0.3
// @description  Export patent information to CSV format
// @author       Your Name
// @match        *://*.cponline.cnipa.gov.cn/*
// @license      MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 创建导出按钮
    const exportButton = document.createElement('button');
    exportButton.textContent = '导出专利信息';
    exportButton.style.position = 'fixed';
    exportButton.style.top = '120px';
    exportButton.style.right = '20px';
    exportButton.style.zIndex = '9999';
    exportButton.style.padding = '10px 20px';
    exportButton.style.fontSize = '16px';
    exportButton.style.backgroundColor = '#4CAF50';
    exportButton.style.color = 'white';
    exportButton.style.border = '1px solid #ddd';
    exportButton.style.borderRadius = '5px';
    exportButton.style.boxShadow = '2px 2px 5px rgba(0,0,0,0.2)';
    exportButton.style.cursor = 'pointer';
    exportButton.addEventListener('click', function() {
        // 创建一个数组来保存提取的信息
        let patentsData = [];

        // 遍历每个专利信息元素
        document.querySelectorAll('.table_info').forEach(function(patent) {
            // 提取元素内部的各项信息
            const spans = patent.querySelectorAll('span');
            let Application_OR_Patent_Number = 'CN' + patent.querySelector('.hover_active').textContent.trim();
            Application_OR_Patent_Number = Application_OR_Patent_Number.slice(0, -1) + '.' + Application_OR_Patent_Number.slice(-1);
            const title = `"${patent.querySelector('span span').textContent.trim()}"`;
            let Applicant = `"${spans[6].textContent.trim().replace("申请人:", "")}"`;
            let PatentType = `"${spans[8].textContent.trim().replace("专利类型:", "")}"`;
            let ApplicationDate = `"${spans[10].textContent.trim().replace("申请日:", "")}"`;
            let AuthorizationAnnouncementNumber = `"${spans[14].textContent.trim().replace("授权公告号:", "")}"`;
            let legalStatus = `"${spans[16].textContent.trim().replace("法律状态:", "")}"`;
            let CaseStatus = `"${spans[18].textContent.trim().replace("案件状态:", "")}"`;
            let AuthorizationAnnouncementDate = `"${spans[20].textContent.trim().replace("授权公告日:", "")}"`;

            // 将提取的信息添加到数组中
            patentsData.push({
                '申请号': Application_OR_Patent_Number,
                '发明名称': title,
                '申请(专利权)人': Applicant,
                '文献类型': PatentType,
                '申请日': ApplicationDate,
                '授权公告号': AuthorizationAnnouncementNumber,
                '法律状态': legalStatus,
                '案件状态': CaseStatus,
                '授权公告日': AuthorizationAnnouncementDate
            });
        });

        // 导出数据到 Excel 表格
        if (patentsData.length > 0) {
            exportToExcel(patentsData);
        } else {
            console.error('没有可导出的专利信息!');
        }
    });
    document.body.appendChild(exportButton);

    // 导出数据到 Excel 表格
    function exportToExcel(data) {
        const header = Object.keys(data[0]);
        const rows = data.map(obj => header.map(key => obj[key]));

        const csvContent = header.join(',') + '\n' + rows.map(row => row.join(',')).join('\n');
        const blob = new Blob([new Uint8Array([0xEF, 0xBB, 0xBF]), csvContent], { type: 'text/csv;charset=utf-8' });
        const url = URL.createObjectURL(blob);

        // 使用浏览器的下载功能下载 CSV 文件
        const a = document.createElement('a');
        a.href = url;
        a.download = 'patent_info.csv';
        a.click();
    }
})();