-
Notifications
You must be signed in to change notification settings - Fork 104
feat: Migrate developer hovercard info to Gitee #970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
wangyantong2000
merged 10 commits into
hypertrons:master
from
Xsy41:feature/gitee-developer-hovercard
Mar 8, 2025
+199
−0
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c46da9f
feat: Migrate repo labels to Gitee
Xsy41 2d62b46
feat: Migrate developer hovercard to Gitee
Xsy41 a1d5d2f
fix
Xsy41 bfae0e6
fix
Xsy41 a5b860c
fix: Fixed the timer issue and removed unnecessary parameters.
Xsy41 6f161a4
Merge branch 'hypertrons:master' into feature/gitee-developer-hovercard
Xsy41 423c19f
feat: Fixed openrank display for different home pages
Xsy41 ff9256a
Merge branch 'hypertrons:master' into feature/gitee-developer-hovercard
Xsy41 bfd45ec
fix: Fixed a bug where openrank would not appear on the first mouse h…
Xsy41 821d662
fix: Remove unnecessary code
Xsy41 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
137 changes: 137 additions & 0 deletions
137
src/pages/ContentScripts/features/developer-hovercard-info/gitee-index.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import features from '../../../../feature-manager'; | ||
| import { getOpenrank } from '../../../../api/developer'; | ||
| import React from 'react'; | ||
| import View from './gitee-view'; | ||
| import { createRoot } from 'react-dom/client'; | ||
| import { getPlatform } from '../../../../helpers/get-platform'; | ||
| import isGitee from '../../../../helpers/is-gitee'; | ||
| const featureId = features.getFeatureID(import.meta.url); | ||
| let isInitialized = false; | ||
| let platform: string; | ||
|
|
||
| const getDeveloperLatestOpenrank = async (developerName: string): Promise<string | null> => { | ||
| const data = await getOpenrank(platform, developerName); | ||
| if (data) { | ||
| const monthKeys = Object.keys(data).filter((key) => /^\d{4}-\d{2}$/.test(key)); | ||
| if (monthKeys.length === 0) { | ||
| return null; | ||
| } | ||
| monthKeys.sort((a, b) => new Date(a).getTime() - new Date(b).getTime()); | ||
| const latestMonthKey = monthKeys[monthKeys.length - 1]; | ||
| return data[latestMonthKey]; | ||
| } | ||
| return null; | ||
| }; | ||
|
|
||
| const waitForValidPopover = async (signal: AbortSignal): Promise<HTMLElement | null> => { | ||
| return new Promise((resolve) => { | ||
| const observer = new MutationObserver((mutations) => { | ||
| try { | ||
| for (const mutation of mutations) { | ||
| if (mutation.attributeName === 'class') { | ||
| const target = mutation.target as HTMLElement; | ||
|
|
||
| if ( | ||
| target.classList.contains('popper-profile-card') && | ||
| !target.classList.contains('hidden') && | ||
| target.querySelector('.popper-profile-card__footer') | ||
| ) { | ||
| observer.disconnect(); | ||
| resolve(target); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
| } catch (error) { | ||
| observer.disconnect(); | ||
| } | ||
| }); | ||
|
|
||
| const initialCheck = document.querySelector('.popper-profile-card:not(.hidden)'); | ||
| if (initialCheck?.querySelector('.popper-profile-card__footer')) { | ||
| resolve(initialCheck as HTMLElement); | ||
| return; | ||
| } | ||
| observer.observe(document.body, { | ||
| attributeFilter: ['class'], | ||
| subtree: true, | ||
| childList: false, | ||
| }); | ||
| }); | ||
| }; | ||
|
|
||
| const processElement = async (element: Element) => { | ||
| const developername = element.getAttribute('data-username'); | ||
| if (!developername) return; | ||
|
|
||
| let abortController = new AbortController(); | ||
|
|
||
| element.addEventListener('mouseover', async () => { | ||
| abortController.abort(); | ||
| abortController = new AbortController(); | ||
| const signal = abortController.signal; | ||
|
|
||
| const popover = (await Promise.race([ | ||
| waitForValidPopover(signal), | ||
| new Promise((_, reject) => setTimeout(() => reject(new Error('Timeout')), 1500)), | ||
| ])) as HTMLElement; | ||
|
|
||
| const cardUsername = popover.querySelector('.username')?.textContent?.replace('@', ''); | ||
| if (cardUsername !== developername) return; | ||
| const existing = popover.querySelector(`[data-username="${developername}"]`); | ||
| if (existing) return; | ||
|
|
||
| const openrank = await getDeveloperLatestOpenrank(developername); | ||
| if (!openrank) { | ||
| return; | ||
| } | ||
| const existingOpenRank = popover.querySelector('.hypercrx-openrank-info'); | ||
| if (existingOpenRank) return; | ||
|
|
||
| const footer = popover.querySelector('.popper-profile-card__content') as HTMLElement; | ||
| if (footer) { | ||
| const openrankContainer = document.createElement('div'); | ||
| openrankContainer.dataset.username = developername; | ||
| if (footer && footer.parentNode) { | ||
| footer.appendChild(openrankContainer); | ||
| } | ||
| createRoot(openrankContainer).render(<View {...{ developerName: developername, openrank }} />); | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| const init = async (): Promise<void> => { | ||
| platform = getPlatform(); | ||
| if (isInitialized) return; | ||
| isInitialized = true; | ||
| const hovercardSelector = 'a.js-popover-card[data-username]'; | ||
|
|
||
| const processExisting = () => { | ||
| document.querySelectorAll(hovercardSelector).forEach((element) => { | ||
| if (!element.hasAttribute('data-hypercrx-processed')) { | ||
| element.setAttribute('data-hypercrx-processed', 'true'); | ||
| processElement(element); | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| const observer = new MutationObserver((mutations) => { | ||
| mutations.forEach((mutation) => { | ||
| if (mutation.type === 'childList') { | ||
| processExisting(); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| observer.observe(document.body, { | ||
| childList: true, | ||
| subtree: true, | ||
| attributes: false, | ||
| }); | ||
| }; | ||
|
|
||
| features.add(featureId, { | ||
| asLongAs: [isGitee], | ||
| awaitDomReady: false, | ||
| init, | ||
| }); | ||
30 changes: 30 additions & 0 deletions
30
src/pages/ContentScripts/features/developer-hovercard-info/gitee-view.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import React from 'react'; | ||
| import '../../../../helpers/i18n'; | ||
|
|
||
| interface OpenRankProps { | ||
| developerName: string; | ||
| openrank: string; | ||
| } | ||
|
|
||
| const View: React.FC<OpenRankProps> = ({ developerName, openrank }) => { | ||
| const textColor = '#636c76'; | ||
| const fontSize = '12px'; | ||
|
|
||
| return ( | ||
| <div className={`hypercrx-openrank-info`} data-developer-name={developerName}> | ||
| <span | ||
| style={{ | ||
| display: 'inline-block', | ||
| verticalAlign: 'middle', | ||
| lineHeight: '1.25 !important ', | ||
| color: textColor, | ||
| fontSize: fontSize, | ||
| }} | ||
| > | ||
| OpenRank {openrank} | ||
| </span> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default View; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the
signalparameter set when it is not usedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok