Cocos Github UserName

Github UserName for Cocos

  1. // ==UserScript==
  2. // @name Cocos Github UserName
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2.1
  5. // @description Github UserName for Cocos
  6. // @author YuanYuan
  7. // @match https://github.com/cocos/*
  8. // @icon https://forum.cocos.org/uploads/default/original/3X/7/a/7ac704385ca592fd41be29b0dff29dd20884c58d.png
  9. // @grant GM_xmlhttpRequest
  10. // @grant GM_download
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15. const customUsrName = {
  16. 'yanOO1497': '编辑器-严媛媛',
  17. };
  18. function getUserNameMap() {
  19. return new Promise(function(resolve, reject) {
  20. let nameMap = localStorage.getItem('cocos-username');
  21. if (nameMap) {
  22. try {
  23. nameMap = Object.assign(JSON.parse(nameMap), customUsrName);
  24. return resolve(nameMap);
  25. } catch (error) {
  26. console.log(error);
  27. }
  28. }
  29. GM_xmlhttpRequest({
  30. method: "GET",
  31. url: 'https://download.cocos.org/CocosTest/yww/userName.json',
  32. responseType: 'json',
  33. onload: function(data) {
  34. localStorage.setItem('cocos-username', JSON.stringify(data.response));
  35. resolve(Object.assign(data.response, customUsrName));
  36. }
  37. });
  38. })
  39. }
  40.  
  41. let promise = getUserNameMap();
  42.  
  43. function onLoad() {
  44. console.log('enter Github UserName');
  45. promise.then((userNameMap) => {
  46. changeUserName(userNameMap);
  47. const $container = document.querySelector('#js-repo-pjax-container');
  48. if (!$container) {
  49. console.error('#js-repo-pjax-container');
  50. return;
  51. }
  52. $container.addEventListener('pjax:end', function () {
  53. console.debug('cocos username refresh:pjax:end');
  54. changeUserName(userNameMap);
  55. })
  56. });
  57. }
  58.  
  59. function changeUserName(userNameMap) {
  60. const $ids = document.querySelectorAll(`a[data-octo-dimensions="link_type:self"]`);
  61. $ids.forEach(($item) => {
  62. $item.innerText = userNameMap[$item.innerText] || $item.innerText;
  63. });
  64. }
  65. window.onload = onLoad;
  66. window.onbeforeunload = function(event){
  67. console.log('onbeforeunload');
  68. }
  69. })();