国家职业技能标准下载
// ==UserScript==
// @name 国家职业技能标准下载
// @namespace http://tampermonkey.net/
// @version 0.1.1
// @description 国家职业技能标准下载
// @author Shuai Ma
// @match http://biaozhun.osta.org.cn/pdfview.html?code=*
// @grant GM_notification
// ==/UserScript==
(function() {
'use strict';
// 获取请求链接中的code值
function getCodeFromUrl() {
const url = window.location.href;
const codeRegex = /code=(\d+)/;
const match = url.match(codeRegex);
return match ? match[1] : '';
}
// 拼接请求链接和code值
function buildApiUrl(code) {
return `http://biaozhun.osta.org.cn/api/v1/profession/get/${code}`;
}
// 发送请求并获取返回的data
async function fetchData(url) {
const response = await fetch(url);
const data = await response.json();
return data;
}
// 对data进行base64解密
function base64Decode(data) {
const decodedData = atob(data);
const byteArray = new Uint8Array(decodedData.length);
for (let i = 0; i < decodedData.length; i++) {
byteArray[i] = decodedData.charCodeAt(i);
}
return byteArray;
}
// 生成pdf文件并下载
function downloadPdf(data, fileName) {
const blob = new Blob([data], { type: 'application/pdf' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = fileName;
link.click();
}
// 主函数
async function main() {
const code = getCodeFromUrl();
const apiUrl = buildApiUrl(code);
const data = await fetchData(apiUrl);
if(data.code == "0"){
GM_notification({
title: "职业标准系统",
text: "下载PDF文件",
onclick: function () {
// 获取iframe元素
var iframe = document.getElementById('pdfFrame');
// 获取iframe中的指定元素
var targetElement = iframe.contentWindow.document.querySelector("#viewer > div:nth-child(1) > div.textLayer > span:nth-child(24)");
// 获取元素的值
var value = targetElement.innerHTML;
console.log(value);
const decodedData = base64Decode(data.data);
downloadPdf(decodedData, value +'.pdf');
}
})
}
}
main();
})();