|
2 | 2 | // @name Show all contributions by year in the GitHub profile
|
3 | 3 | // @name:zh-CN 在 GitHub profile 页面以年份展示用户所有的贡献
|
4 | 4 | // @namespace https://github.com/kang8
|
5 |
| -// @version 0.0.4 |
| 5 | +// @version 0.0.5 |
6 | 6 | // @updateURL https://raw.githubusercontent.com/kang8/.dotfiles/master/tampermonkey-scripts/show-all-contributions.js
|
7 | 7 | // @downloadURL https://raw.githubusercontent.com/kang8/.dotfiles/master/tampermonkey-scripts/show-all-contributions.js
|
8 | 8 | // @description Show all contributions by year since the user was created in the GitHub profile page
|
|
18 | 18 | (async function () {
|
19 | 19 | 'use strict';
|
20 | 20 |
|
21 |
| - // Wait for 1.5 seconds for the DOM to load. |
22 |
| - await new Promise((resolve) => setTimeout(resolve, 1500)); |
| 21 | + const contributionsLastYearSelector = 'div.js-yearly-contributions > div:nth-child(1)' |
| 22 | + |
| 23 | + await waitForElement(contributionsLastYearSelector); |
23 | 24 |
|
24 | 25 | const pathArr = location.pathname.split('/');
|
25 | 26 |
|
|
34 | 35 | ) {
|
35 | 36 | const userId = pathArr[1];
|
36 | 37 |
|
37 |
| - const contributionsLastYear = document.querySelector( |
38 |
| - 'div.js-yearly-contributions > div:nth-child(1)', |
39 |
| - ); |
| 38 | + const contributionsLastYear = document.querySelector(contributionsLastYearSelector); |
40 | 39 |
|
41 | 40 | const contributionsAllYearsDiv = document.createElement('div');
|
42 | 41 | contributionsAllYearsDiv.className = 'position-relative border';
|
@@ -148,3 +147,26 @@ function getYearByContributionsCalendar(childNode) {
|
148 | 147 | '',
|
149 | 148 | ).slice(-4);
|
150 | 149 | }
|
| 150 | + |
| 151 | +/** |
| 152 | + * @param {string} selector |
| 153 | + */ |
| 154 | +function waitForElement(selector) { |
| 155 | + return new Promise((resolve) => { |
| 156 | + if (document.querySelector(selector)) { |
| 157 | + return resolve(document.querySelector(selector)); |
| 158 | + } |
| 159 | + |
| 160 | + const observer = new MutationObserver(() => { |
| 161 | + if (document.querySelector(selector)) { |
| 162 | + observer.disconnect(); |
| 163 | + resolve(document.querySelector(selector)); |
| 164 | + } |
| 165 | + }); |
| 166 | + |
| 167 | + observer.observe(document.body, { |
| 168 | + childList: true, |
| 169 | + subtree: true |
| 170 | + }); |
| 171 | + }); |
| 172 | +}; |
0 commit comments