网页标题笔记

将选中文字添加到网页标题最前面,以便在浏览器标签栏快速识别

  1. // ==UserScript==
  2. // @name 网页标题笔记
  3. // @version 0.1.0.1
  4. // @icon https://tampermonkey.net/favicon.ico
  5. // @description 将选中文字添加到网页标题最前面,以便在浏览器标签栏快速识别
  6. // @author You!
  7. // @grant unsafeWindow
  8. // @include *
  9. // @run-at document-start
  10. // @namespace https://gf.qytechs.cn/zh-CN/scripts/369580-网页标题笔记
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. document.addEventListener('keydown', function() {
  15. if (192 !== event.keyCode) return;
  16.  
  17. if (event.ctrlKey && !event.altKey && !event.shiftKey) setTitleNote(); //替换添加:Ctrl
  18. else if (!event.ctrlKey && event.altKey && !event.shiftKey) setTitleNote(true); //正向添加:Alt
  19. else if (event.ctrlKey && event.altKey && !event.shiftKey) setTitleNote(false); //反向添加:Ctrl + Alt
  20. else if (event.ctrlKey && !event.altKey && event.shiftKey) clearTitleNote(); //清除笔记:Ctrl + Shift
  21.  
  22. function getSelectionTxt() {
  23. var txt = unsafeWindow.getSelection().toString().trim();
  24. return txt.length ? txt : null;
  25. }
  26. function getNote(txt, append) {
  27. if ('boolean' !== typeof append) {
  28. unsafeWindow.txts = [txt];
  29. return txt;
  30. } else {
  31. if (undefined === unsafeWindow.txts) unsafeWindow.txts = [];
  32. if (append) unsafeWindow.txts.push(txt);
  33. else unsafeWindow.txts.unshift(txt);
  34. }
  35. return unsafeWindow.txts.join('|');
  36. }
  37. function getOriginalTitle() {
  38. if (undefined === unsafeWindow.title0) unsafeWindow.title0 = document.title;
  39. return unsafeWindow.title0;
  40. }
  41. function setTitleNote(append) {
  42. var txt = getSelectionTxt();
  43. if (!txt) return;
  44. document.title = getNote(txt, append) + '▶' + getOriginalTitle();
  45. }
  46. function clearTitleNote() {
  47. if (undefined !== unsafeWindow.title0) document.title = unsafeWindow.title0;
  48. delete unsafeWindow.txts;
  49. }
  50. });
  51. }());

QingJ © 2025

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