// ==UserScript== // @name 自定义格式化文献 // @namespace https://bbs.tampermonkey.net.cn/ // @version 0.2.3 // @description 目前支持sciencedirect,rsc和springer, onlinelibrary.wiley.com // @author tfsn20 // @match https://www.sciencedirect.com/* // @match https://pubs.rsc.org/* // @match https://link.springer.com/* // @match https://onlinelibrary.wiley.com/* // @match https://webvpn.zzuli.edu.cn/https/* // @grant GM_getValue // @grant GM_setValue // ==/UserScript== /* ==UserConfig== 自定义格式化参数: figAnnotation: title: 是否保留图片注释 description: 是否保留图片注释, 勾选时会保留 type: checkbox default: false tableRelated: title: 是否保留表格相关 description: 是否保留表格相关, 勾选时会保留 type: checkbox default: false periodToOneDotLeader: title: 涉及图指, 公式指, 表格指, 标题句点转换为一点前导符, 在标题和关键词末尾加句点; 删除表注和表格 description: 此选项用于深度学习时分割句子的区别, 勾选时请将1, 2, 4, 5 取消勾选 type: checkbox default: false title: title: 是否保留序列标题 description: 不是论文题目,而是每一部分的小标题 type: checkbox default: true ==/UserConfig== */ (function () { 'use strict'; let figAnnotation = GM_getValue('自定义格式化参数.figAnnotation'); let periodToOneDotLeader = GM_getValue('自定义格式化参数.periodToOneDotLeader'); let tableRelated = GM_getValue('自定义格式化参数.tableRelated'); let title = GM_getValue('自定义格式化参数.title'); let match = window.location.pathname.match(/(?:\/[^\/]*){1}\/([^\/]*)/); let webvpnPara = match ? match[1] : null; // 使用 JavaScript 动态添加 `], { type: 'text/plain' }); var a = document.createElement('a'); a.href = window.URL.createObjectURL(blob); a.download = `${fileName}.html`; document.body.appendChild(a); a.click(); document.body.removeChild(a); /* doc */ var blob = new Blob(['' + text.replace(/(.*)/mg, '

$1

') + ''], { type: 'application/msword;charset=utf-8' }); var a = document.createElement('a'); a.href = window.URL.createObjectURL(blob); a.download = `${fileName}.doc`; document.body.appendChild(a); a.click(); document.body.removeChild(a); /* docx */ var text_ = text.match(/(^.*$)/mg); var _ = [] for (var i = 0, len = text_.length; i < len; i++) { _.push(new docx.Paragraph({ children: [ new docx.TextRun(text_[i])] })) } const doc = new docx.Document({ sections: [{ properties: {}, children: _, }] }); docx.Packer.toBlob(doc).then(blob => { var a = document.createElement('a'); a.href = window.URL.createObjectURL(blob); a.download = `${fileName}.docx`; document.body.appendChild(a); a.click(); document.body.removeChild(a); }); } if (window.location.host == 'www.sciencedirect.com'||webvpnPara=='77726476706e69737468656265737421e7e056d234336155700b8ca891472636a6d29e640e') { button = createButton('(235, 101, 0, 1)', '(228, 229, 229, 0.5)') button.addEventListener('click', function () { // 获取下载文献的名称 let fileName = document.querySelector('article').querySelector('h1').innerText; // 获取所有classname为'download-link'的元素 document.querySelectorAll('.download-link').forEach((e)=>{e.parentNode.removeChild(e)}); if (!figAnnotation) document.querySelectorAll("Figure").forEach((e)=>{e.parentNode.removeChild(e)}); // 为标题末尾增加句点并替换原句点为一点前导符 if (periodToOneDotLeader) { document.querySelector('article').querySelectorAll('h1, h2, h3, h4, h5').forEach(e => {e.innerText = e.innerText.replace(/\./g, '․') + '.'}); //为图指, 方程指,表指替换一点前导符, 下面的方法不通用 // var elements = document.querySelector('article').querySelectorAll('a'); // for (var i = 0; i < elements.length; i++) { // if (elements[i].href.includes('#fig') || elements[i].href.includes('#eqn') || elements[i].href.includes('#tbl')) { // elements[i].innerText = elements[i].innerText.replace(/\./g, '․'); // } // } } //删除表注和表格, 存在仅仅字母t开头的列表 if (!tableRelated) document.querySelectorAll('[id^="t"]').forEach((e)=>{if(e.id.match(/^t.*\d+$/)) e.parentNode.removeChild(e)}); // 替换img alt 'radical dot'为\u2022 • document.querySelectorAll("img").forEach((e)=>{ if (e.alt == 'radical dot') e.parentNode.replaceChild(document.createTextNode('•'), e) }) if(!title) document.querySelector("#body>div").querySelectorAll('h1, h2, h3, h4, h5').forEach((e)=>{e.parentNode.removeChild(e)}); var text = `${formatInnerText('article h1')}\n ${formatInnerText("#abstracts")}\n ${(() => {// 获取包含关键词的父元素 var keywordsContainer = document.querySelector('.Keywords'); // 获取关键词节点列表 var keywordNodes = keywordsContainer.querySelectorAll('.keyword'); // 初始化一个空字符串,用于存储结果文本 // var resultText = 'Keywords\n\n'; var resultText = keywordsContainer.querySelector('h2').innerText + '\n\n'; // 遍历关键词节点列表 for (var i = 0, len = keywordNodes.length; i < len; i++) { var keywordText = keywordNodes[i].querySelector('span').innerText; // 拼接到结果字符串,并添加换行符 if (i == len - 1) { resultText += keywordText; break } resultText += keywordText + '; '; } if (periodToOneDotLeader) { return resultText + '.' } else { return resultText } })()}\n ${replaceNewlinesWithCondition(formatInnerText("#body>div"))}`; if(periodToOneDotLeader) text=text.replace(/(Figs?)\./g,'$1․').replace(/(E|eqs?)\./g,'$1․').replace(/(et al)\./g,'$1․').replace(/e\.g\./g,'e․g․'); downDoc(text, fileName); }); } else if (window.location.host == 'pubs.rsc.org') { // '(238,220,0,1)'黄色 '(72,169,197)'淡蓝 '(0,73,118)'深蓝 '(151,215,0)'草绿 var button = createButton('(255,255,255,1)', '(239,51,64,0.9)'); button.addEventListener('click', function () { // 去除论文作者 document.querySelectorAll('.article__authors').forEach((e)=>{e.parentNode.removeChild(e)}); // 去除a标签(主要去除引用) document.querySelectorAll('a').forEach((e)=>{e.parentNode.removeChild(e)}); var text =formatInnerText(document.querySelector("article").innerText.replace(/\n\nConflicts of interest[\s\S]*/g, '')); if(periodToOneDotLeader) text=text.replace(/(Figs?)\./g,'$1․').replace(/(E|eqs?)\./g,'$1․').replace(/(et al)\./g,'$1․').replace(/e\.g\./g,'e․g․'); downDoc(text); }) } else if (window.location.host == 'link.springer.com' || webvpnPara == '77726476706e69737468656265737421fcfe4f976923784277068ea98a1b203a54') { var button = createButton('(27,48,81,0.9)', '(206,219,224,0.8)'); button.addEventListener('click', function () { var fileName = document.querySelector('h1').innerText; //清空结论后的元素 var flag=false; var elements = document.querySelector('.main-content').children; for (let i = 0; i < elements.length; i++) { if(!flag){ if(elements[i].getAttribute('data-title')=='Conclusion'){ flag=!flag } }else{ elements[i].innerHTML='' } } //图片注释 if(!figAnnotation) document.querySelectorAll('[data-test="figure"]').forEach((e)=>{e.parentNode.removeChild(e)}); //表格 if(!tableRelated) document.querySelectorAll('[id*=table-]').forEach((e)=>{e.parentNode.removeChild(e)}); // 小标题加句点 if(periodToOneDotLeader) document.querySelector('#main').querySelectorAll('h1, h2, h3, h4, h5').forEach((e)=>{e.innerText = e.innerText.replace(/\./g, '․') + '.'}); // 小标题 if(!title) document.querySelector('#main').querySelectorAll('h1, h2, h3, h4, h5').forEach((e)=>{e.parentNode.removeChild(e)}); var text = `${formatInnerText('h1[data-test="article-title"]')}\n ${formatInnerText('section[data-title="Abstract"]')}\n ${formatInnerText('.main-content')}` if(periodToOneDotLeader) text=text.replace(/(Figs?)\./g,'$1․').replace(/(E|eqs?)\./g,'$1․').replace(/(et al)\./g,'$1․').replace(/e\.g\./g,'e․g․'); downDoc(text,fileName); }); } else if (window.location.host == 'onlinelibrary.wiley.com'){ var button = createButton('(27,48,81,0.9)', '(206,219,224,0.8)'); button.addEventListener('click', function () { const fileName=document.querySelector('.citation__title').innerText; var sons=document.querySelector('section[class*="article-section__full"]').children; var flag=false // 遍历并使一些子元素innerHtml为空 for (var i = 0; i < sons.length; i++) { if(!flag){ if(sons[i].tagName=='SECTION'){ // console.log(i) }else{ flag=true sons[i].innerHTML='' } }else{ sons[i].innerHTML='' } } if(periodToOneDotLeader) document.querySelectorAll('h1, h2, h3, h4, h5').forEach(e => {e.innerText = e.innerText.replace(/\./g, '․') + '.'}); if(!figAnnotation) document.querySelectorAll('.article-section__inline-figure').forEach((e)=>{e.parentNode.removeChild(e)}); if(!tableRelated) document.querySelectorAll('.article-table-content').forEach((e)=>{e.parentNode.removeChild(e)}); // 删除span标签, 去除引用 document.querySelector('section[class*="article-section__full"]').querySelectorAll('span').forEach((e)=>{e.parentNode.removeChild(e)}); if(!title) document.querySelector('section[class*="article-section__full"]').querySelectorAll('h1, h2, h3, h4, h5').forEach((e)=>{e.parentNode.removeChild(e)}); var text = `${formatInnerText('.citation__title')}\n ${formatInnerText('div[class*="abstract-group"]')}\n ${formatInnerText('section[class*="article-section__full"]')}` if(periodToOneDotLeader) text=text.replace(/(Figs?)\./g,'$1․').replace(/(E|eqs?)\./g,'$1․').replace(/(et al)\./g,'$1․').replace(/e\.g\./g,'e․g․'); downDoc(text,fileName); }); } var style = document.createElement('style'); style.textContent = ` @keyframes blink { 0% { opacity: 1; } 50% { opacity: 0; } 100% { opacity: 1; } } #blinkingButton { animation: blink 1s infinite; } `; document.head.appendChild(style); })();