Reference Generator

Automatically generate reference strings of the page

目前为 2023-06-18 提交的版本。查看 最新版本

// ==UserScript==
// @name         Reference Generator
// @name:zh-CN   参考文献生成器
// @description  Automatically generate reference strings of the page
// @description:zh-CN 自动生成页面参考文献引用字符串
// @author       KazariEX
// @version      1.0.0
// @license      MIT
// @copyright    2023-2023, References Generator Contributors (https://github.com/MysteryBao37/reference-generator/graphs/contributors)
// @namespace    http://github.com/MysteryBao37
// @homepageURL  https://github.com/MysteryBao37/reference-generator

// @match        *://www.cppcc.gov.cn/zxww/*/*/*/*.shtml
// @match        *://www.guancha.cn/*/*.shtml
// @match        *://www.npc.gov.cn/*/*/*.shtml

// ==/UserScript==
"use strict";

const typelist = [
    "DB/OL",
    "DB/MT",
    "DB/CD",
    "M/CD",
    "CP/DK",
    "J/OL",
    "EB/OL"
];

const sites = {
    "www.cppcc.gov.cn": {
        date: {
            selector: "#page_body > div.column_wrapper.ariaskiptheme > div.col_w770.ariaskiptheme > div > div > div.infobox.ariaskiptheme > span.info.ariaskiptheme > i",
            re: /([0-9]*)-([0-9]*)-([0-9]*)/,
            index: 0
        },
        src: {
            selector: "#page_body > div.column_wrapper.ariaskiptheme > div.col_w770.ariaskiptheme > div > div > div.infobox.ariaskiptheme > span.info.ariaskiptheme > em",
            re: /^来源:(.*)$/,
            index: 1
        }
    },
    "www.guancha.cn": {
        date: {
            selector: "body > div.content > div.main.content-main > ul > li.left.left-main > div.time.fix > span:nth-child(1)",
            re: /([0-9]*)-([0-9]*)-([0-9]*)/,
            index: 0
        },
        src: {
            selector: "body > div.content > div.main.content-main > ul > li.left.left-main > div.time.fix > span:nth-child(3)",
            re: /^来源:(.*)$/,
            index: 1
        }
    },
    "www.npc.gov.cn": {
        date: {
            selector: "body > div.wrapper > div > div.fl.w680 > div.fontsize > span",
            re: /([0-9]*)\u5e74([0-9]*)\u6708([0-9]*)\u65e5/,
            index: 0
        },
        src: {
            selector: "body > div.wrapper > div > div.fl.w680 > div.fontsize",
            re: /来源: (.*?)\s\s/,
            index: 1
        }
    }
};

function siteHandler(site)
{
    const $Date = document.querySelector(site.date.selector);
    const $Source = document.querySelector(site.src.selector);

    return {
        date: $Date.textContent.match(site.date.re)[site.date.index],
        author: $Source.textContent.match(site.src.re)[site.src.index]
    };
}

(function main() {

    const btn = document.createElement("button");
    btn.textContent = "生成文献引用";
    btn.style.cssText = `
    display: block;
    position: fixed;
    top: 8px;
    right: 8px;
    padding: 4px 12px;
    outline: none;
    box-shadow: 0 0 4px rgb(0 0 0 / 33%);
    border: 0;
    border-radius: 4px;
    background-color: rgb(255 255 255 / 88%);
    cursor: pointer;
    z-index: 9999999;
    `;

    btn.addEventListener("click", handler);

    document.body.appendChild(btn);
})();

function handler()
{
    let type = typelist[6];
    let title = document.title;
    let author;
    let date;

    const $MetaList = document.querySelectorAll("head > meta");
    $MetaList.forEach(($Meta) => {
        //作者
        if ($Meta.name === "author" || $Meta.getAttribute("property")?.includes("author")) {
            author = $Meta.content;
        }
        //标题
        if ($Meta.getAttribute("property")?.includes("title")) {
            title = $Meta.content;
        }
    });

    //网站特化
    const host = window.location.host;
    if (host in sites) {
        ({
            date,
            author
        } = siteHandler(sites[host]));
    };

    //引用日期
    const now = new Date()
    .toLocaleDateString()
    .split("/")
    .map((item, index) => {
        const len = index === 0 ? 4 : 2;
        return item.padStart(len, "0");
    }).join('-');

    //拼接结果
    const result = `[1] ${author}. ${title}[${type}]. (${date})[${now}] ${window.location.href}`;

    //显示结果
    alert(result);
};

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址