Skip to content

Commit d8114b0

Browse files
feat: added fetch branch function using URL and DOM
1 parent 7b3f7b8 commit d8114b0

File tree

6 files changed

+112
-241
lines changed

6 files changed

+112
-241
lines changed

src/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
export const BASE_URL = "https://ilovegithub.oderna.in";
2-
export const LOGO_URL =
3-
"https://raw.githubusercontent.com/crackedngineer/ilovegithub/master/public/icons/favicon.png";
42

53
// DOM IDs and Classes
64
export const DOM_IDS = {
@@ -44,3 +42,5 @@ export const MESSAGE_TYPES = {
4442

4543
// Environment constants
4644
export const IS_DEVELOPMENT = process.env.NODE_ENV === "development";
45+
46+
export const DEFAULT_BRANCH = "master";

src/content.ts

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { getRepoInfo } from "./helpers";
1+
import {
2+
getBranchFromDOM,
3+
getBranchFromUrl,
4+
getRepoInfo,
5+
isRepoPage,
6+
} from "./helpers";
27
import { RepoInfo, Tool } from "./types";
38
import { loadTools } from "./utils";
49

@@ -14,7 +19,20 @@ class WebIdeExtension {
1419
async init(): Promise<void> {
1520
try {
1621
this.hasPackageJson = this.checkPackageJson();
17-
this.repoInfo = getRepoInfo();
22+
// check if on a repo page
23+
if (isRepoPage() === false) {
24+
console.error("Not a repository page. Exiting.");
25+
return;
26+
}
27+
const info = getRepoInfo();
28+
if (!info) {
29+
console.error("Failed to extract repository info. Exiting.");
30+
return;
31+
}
32+
this.repoInfo = {
33+
...info,
34+
branch: getBranchFromUrl() || getBranchFromDOM(),
35+
};
1836

1937
this.tools = await loadTools(this.repoInfo);
2038

@@ -38,23 +56,6 @@ class WebIdeExtension {
3856
return true;
3957
}
4058

41-
// private async addStyles(): Promise<void> {
42-
// try {
43-
// // Read CSS file
44-
// const cssUrl = chrome.runtime.getURL("styles/content.css");
45-
// const response = await fetch(cssUrl);
46-
// const cssText = await response.text();
47-
48-
// // Inject into page
49-
// const styleElement = document.createElement("style");
50-
// styleElement.id = "web-ide-styles";
51-
// styleElement.textContent = cssText;
52-
// document.head.appendChild(styleElement);
53-
// } catch (error) {
54-
// console.error("Failed to inject CSS:", error);
55-
// }
56-
// }
57-
5859
private addGitHubSelectMenu(index: number = 1): void {
5960
const selectors = [
6061
".OverviewContent-module__Box_6--wV7Tw",
@@ -70,8 +71,6 @@ class WebIdeExtension {
7071
const filteredItems = this.tools.filter((item) => this.filterItems(item));
7172
if (filteredItems.length === 0) return;
7273

73-
// this.addStyles();
74-
7574
const detailsElement = this.createDropdownElement(filteredItems);
7675

7776
// Insert at specific index
@@ -94,12 +93,21 @@ class WebIdeExtension {
9493

9594
detailsElement.innerHTML = `
9695
<summary role="button" type="button" class="btn text-center">
97-
<span class="d-none d-xl-flex flex-items-center">
96+
<span class="d-none d-xl-flex flex-items-center gap-[0.5rem]">
97+
<img
98+
src="https://aa3d4bqalqflkq22.public.blob.vercel-storage.com/images/icons/symbol.png"
99+
alt="iLoveGithub Tools"
100+
class="web-ide-dropdown-icon"
101+
/>
98102
Open Tools
99103
<span class="dropdown-caret ml-2"></span>
100104
</span>
101105
<span class="d-inline-block d-xl-none">
102-
Tools
106+
<img
107+
src="https://aa3d4bqalqflkq22.public.blob.vercel-storage.com/images/icons/symbol.png"
108+
alt="iLoveGithub Tools"
109+
class="web-ide-dropdown-icon"
110+
/>
103111
<span class="dropdown-caret d-none d-sm-inline-block d-md-none d-lg-inline-block"></span>
104112
</span>
105113
</summary>
@@ -139,7 +147,10 @@ class WebIdeExtension {
139147
target="_blank"
140148
rel="noopener noreferrer"
141149
title="${item.name}">
142-
<img src="${item.icon}" class="tool-icon" alt="${item.name}" />
150+
<img src="${
151+
item?.icon ??
152+
"https://aa3d4bqalqflkq22.public.blob.vercel-storage.com/brain.png"
153+
}" class="tool-icon" alt="${item.name}" />
143154
<span class="web-ide-item-title">${item.name}</span>
144155
</a>
145156
</li>

src/helpers.ts

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
import {IS_DEVELOPMENT, EXCLUDED_GITHUB_PATHS} from "./constants";
2-
import {RepoInfo, Tool} from "./types";
1+
import {
2+
IS_DEVELOPMENT,
3+
EXCLUDED_GITHUB_PATHS,
4+
DEFAULT_BRANCH,
5+
} from "./constants";
36

47
/**
58
* Debug logging utility
@@ -18,47 +21,68 @@ export function isRepoPage(): boolean {
1821
const pathParts = path.split("/").filter((part) => part.length > 0);
1922

2023
if (pathParts.length >= 2) {
21-
return !EXCLUDED_GITHUB_PATHS.includes(pathParts[0] as (typeof EXCLUDED_GITHUB_PATHS)[number]);
24+
return !EXCLUDED_GITHUB_PATHS.includes(
25+
pathParts[0] as (typeof EXCLUDED_GITHUB_PATHS)[number]
26+
);
2227
}
2328
return false;
2429
}
2530

2631
/**
2732
* Extract repository information from URL
2833
*/
29-
export function getRepoInfo(): RepoInfo | null {
34+
export function getRepoInfo(): { owner: string; repo: string } | null {
3035
const path = window.location.pathname;
3136
const pathParts = path.split("/").filter((part) => part.length > 0);
3237

3338
if (pathParts.length >= 2) {
3439
return {
3540
owner: pathParts[0],
3641
repo: pathParts[1],
37-
branch: "main",
3842
};
3943
}
4044
return null;
4145
}
4246

43-
/**
44-
* Format numbers with k/M suffixes
45-
*/
46-
export function formatNumber(num: number): string {
47-
if (num >= 1000000) return (num / 1000000).toFixed(1) + "M";
48-
if (num >= 1000) return (num / 1000).toFixed(1) + "k";
49-
return num.toString();
50-
}
47+
export function getBranchFromUrl(): string | null {
48+
const pathname = window.location.pathname;
5149

52-
/**
53-
* Filter tools based on search query
54-
*/
55-
export function filterTools(tools: Tool[], query: string): Tool[] {
56-
if (!query) return [...tools];
50+
// Extract: /owner/repo/tree/BRANCH_NAME/optional/file/path
51+
const parts = pathname.split("/");
52+
const treeIndex = parts.indexOf("tree");
53+
54+
if (treeIndex !== -1 && treeIndex + 1 < parts.length) {
55+
// Everything between 'tree' and known GitHub sections
56+
const knownSections = [
57+
"blob",
58+
"commits",
59+
"actions",
60+
"issues",
61+
"pull",
62+
"wiki",
63+
];
64+
const branchParts = [];
5765

58-
const lowercaseQuery = query.toLowerCase().trim();
59-
return tools.filter(
60-
(tool) =>
61-
tool.name.toLowerCase().includes(lowercaseQuery) ||
62-
tool.description.toLowerCase().includes(lowercaseQuery),
66+
for (let i = treeIndex + 1; i < parts.length; i++) {
67+
if (knownSections.includes(parts[i]) || parts[i].includes("?")) {
68+
break;
69+
}
70+
branchParts.push(parts[i]);
71+
}
72+
73+
if (branchParts.length > 0) {
74+
return decodeURIComponent(branchParts.join("/"));
75+
}
76+
return null;
77+
}
78+
return null;
79+
}
80+
81+
export function getBranchFromDOM(): string {
82+
const branchSelector = document.querySelector(
83+
"#ref-picker-repos-header-ref-selector > span > span.prc-Button-Label-pTQ3x > div > div.ref-selector-button-text-container.RefSelectorAnchoredOverlay-module__RefSelectorBtnTextContainer--yO402 > span"
6384
);
85+
86+
const branchText = branchSelector?.textContent?.trim();
87+
return branchText || DEFAULT_BRANCH;
6488
}

src/types.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,6 @@ export interface Tool {
1717
icon: string;
1818
}
1919

20-
export interface RepoAnalytics {
21-
stars: number;
22-
forks: number;
23-
issues: number;
24-
language: string;
25-
}
26-
27-
export interface UIState {
28-
isPopupOpen: boolean;
29-
isLoading: boolean;
30-
error: string | null;
31-
searchQuery: string;
32-
filteredTools: Tool[];
33-
}
34-
3520
export interface CreateElementOptions {
3621
id?: string;
3722
className?: string;
@@ -41,13 +26,6 @@ export interface CreateElementOptions {
4126
[key: string]: string | undefined;
4227
}
4328

44-
export interface ExtensionElements {
45-
floatingButton: HTMLElement | null;
46-
popup: HTMLElement | null;
47-
searchInput: HTMLInputElement | null;
48-
toolsList: HTMLElement | null;
49-
}
50-
5129
export interface ToolsApiResponse {
5230
success: boolean;
5331
data: Tool[];

0 commit comments

Comments
 (0)