True Title

获取网页真正的标题,取代页面标题

  1. // ==UserScript==
  2. // @name True Title
  3. // @namespace https://gf.qytechs.cn/zh-CN/users/1073-hzhbest
  4. // @version 0.1
  5. // @description 获取网页真正的标题,取代页面标题
  6. // @author hzhbest
  7. // @include http://*.html
  8. // @include https://*.html
  9. // @grant none
  10. // @license none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // 寻找当前页面body中第一个出现的类标题元素
  17. // 例如 H1、H2、*.~title~、*#~title~
  18. var titleelm = [
  19. "h1",
  20. "h2",
  21. "[class*='title']",
  22. "[id*='title']"
  23. ];
  24. var fsttelm;
  25. // 排除隐藏的或者页面位置太低或者空的元素
  26. for (var i=0;i<titleelm.length;i++){
  27. fsttelm = document.body.querySelector(titleelm[i]); // console.log(titleelm[i] + ", " + fsttelm.textContent);
  28. if (!!fsttelm) {
  29. if (fsttelm.offsetHeight == 0 || fsttelm.offsetTop == 0 || fsttelm.offsetTop > 1000 || fsttelm.textContent.length < 2) {
  30. continue;
  31. } else {
  32. break;
  33. }
  34. }
  35. }
  36.  
  37. // 提取类标题元素中的字符串,检查字符串的字数,如果少于5个字则忽略
  38. var titlestr = fsttelm.textContent;
  39. if (titlestr.length<5) return;
  40. // 检查document.title中是否已含有该字符串最多前10字,否,则替换其为字符串
  41. if (document.title.indexOf(titlestr.slice(0,10)) == -1) {
  42. document.title = titlestr;
  43. }
  44.  
  45. })();

QingJ © 2025

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