更好地一键开启github1s页面

更好地支持文件级用打开github1s

  1. // ==UserScript==
  2. // @name 更好地一键开启github1s页面
  3. // @namespace github
  4. // @author cjm
  5. // @description 更好地支持文件级用打开github1s
  6. // @include https://github.com/
  7. // @match https://github.com/*
  8. // @version 4.1.0
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. function func() {
  13. setTimeout(()=>{
  14. const href = `https://github1s.com${location.pathname}`
  15. const ele1 = document.getElementById('wocao1');
  16. const ele2 = document.getElementById('wocao1');
  17. if(ele1 || ele2){
  18. ele1.href = href;
  19. ele2.href = href;
  20. }else{
  21. const btn1 = `<a id="wocao1" class="btn ml-2 d-none d-md-block" style="
  22. background: #f6f8fa;
  23. color: #2da44e;
  24. display: inline-block !important;
  25. /*width: 48%;*/
  26. text-align: center;
  27. /*margin: 5px;*/
  28. " target="_blank" href="${href}">` + '使用 github1s 打开(新窗口)' + '</a>'
  29. const btn2 = `<a id="wocao2" class="btn ml-2 d-none d-md-block" style=" background: #8c7ae6;
  30. background: #2da44e;
  31. color: #f6f8fa;
  32. display: inline-block !important;
  33. /*width: 48%;*/
  34. text-align: center;
  35. /*margin: 5px;*/
  36. " target="_self" href="${href}">` + '使用 github1s 打开(本页)' + '</a>'
  37. const parent = document.querySelector('div.AppHeader-context-full > nav');//.pagehead-actions //#repository-container-header
  38. //parent?.insertAdjacentHTML('beforeBegin', btn1);
  39. parent?.insertAdjacentHTML('afterend', btn1);
  40. parent?.insertAdjacentHTML('afterend', btn2);
  41. }
  42. }
  43. , 1000)
  44. }
  45. func();
  46.  
  47. //修改native以拦截popstate事件
  48. var pushState = history.pushState;
  49. history.pushState = function() {
  50. var ret = pushState.apply(history, arguments);
  51. window.dispatchEvent(new Event("pushstate"));
  52. window.dispatchEvent(new Event("locationchangefathom"));
  53. return ret;
  54. }
  55.  
  56. window.addEventListener("popstate", function() {
  57. window.dispatchEvent(new Event("locationchangefathom"))
  58. });
  59. window.addEventListener("locationchangefathom", trackPageview)
  60. function trackPageview() {
  61. func();
  62. }