Greasy Fork is available in English.
自定义网页标题+相关工具
// ==UserScript==
// @name 【网页标题助手】
// @namespace http://greasyfork.icu/
// @version 241009.13
// @description 自定义网页标题+相关工具
// @author You
// @license MIT
// @run-at document-end
// @match *://*/*
// @grant GM_registerMenuCommand
// @grant GM_setClipboard
// ==/UserScript==
(function() {
'use strict';const $=(e)=>{return document.querySelector(e)};
let autoTitle=1;/*值为1时,无标题网页自动把域名作为标题*/
const siteRules=[
{
'name':'网站名称',
'match':'匹配url正则。用构造函数创造的正则对象,需要常规的字符转义规则(在前面加反斜杠 \)',
'title':'网页标题选择器/函数'
},
{
'name':'微信公众号文章',
'match':'mp.weixin.qq.com\\/s',
'title':()=>{return document.title+'_'+$('#js_name').innerText.trim();}
},
{
'name':'可爱TV',
'match':'keai.cm\\/\\?.*?id=.*?',
'title':'.text-container'
}
];
function main(){
/*匹配自定义规则*/
for(let i of siteRules){
let ruleReg=new RegExp(i.match);
if(location.href.match(ruleReg)){
let title=typeof i.title=='function'?i.title():$(i.title)?.innerText;
title&&(document.title=title);
break;}
}
!document.title&&autoTitle=='1'&&(document.title=location.hostname);
}
setTimeout(main,1000);
GM_registerMenuCommand('【修改网页标题】',function(){let newTitle=prompt('想要什么标题,随心改',document.title);newTitle&&(document.title=newTitle);});
GM_registerMenuCommand('【复制网页标题】',function(){GM_setClipboard(document.title);});
GM_registerMenuCommand('【复制网页标题和网址】',function(){GM_setClipboard(`${document.title}\n${decodeURIComponent(location.href)}\n\n`);});
})();