Greasy Fork

为无标题网页设置标题

若网页无标题,尝试以h1~h6标签文本内容作为标题;如果也不存在,就把域名作为标题

目前为 2024-09-22 提交的版本。查看 最新版本

// ==UserScript==
// @name         为无标题网页设置标题
// @namespace    https://greasyfork.org/
// @version      240922.13
// @description  若网页无标题,尝试以h1~h6标签文本内容作为标题;如果也不存在,就把域名作为标题
// @author       You
// @license      MIT
// @run-at       document-end
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

const $=(e)=>{return document.querySelector(e)};

function main(){
// 检查网页标题是否为空
if (!document.title) {
	let headings = $('h1')||$('h2')||$('h3')||$('h4')||$('h5')||$('h6')||'';
	document.title=headings?headings.innerText.trim():location.hostname;
}
}
    setTimeout(main,1000);
})();