Skip to content

Commit ce76817

Browse files
authored
Merge pull request #39 from okunamayanad/31-plugins
feat: 🏷️ Add plugin type
2 parents 2850a00 + 73b1681 commit ce76817

File tree

4 files changed

+102
-23
lines changed

4 files changed

+102
-23
lines changed

src/contentScript.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { SearchResultInfo } from './interfaces/searchResultInfo';
12
import { ExtractInfo } from './lib/extractInfo';
3+
import { generateModLink } from './lib/generateModLink';
24
import { MakeModrinthButton } from './lib/makeModrinthButton';
35

46
let searchResults: HTMLElement[] = [];
@@ -30,30 +32,19 @@ searchResults.forEach((element) => {
3032
modrinthButtonContainer.querySelector('.button-modlinker')!;
3133
console.log('modrinthButton', modrinthButton);
3234

33-
const isOnModrinth = await chrome.runtime.sendMessage([
34-
'cacheCheck',
35-
`https://api.modrinth.com/v2/project/${extractedInfo.modId}`,
36-
]);
37-
38-
if (isOnModrinth instanceof Error) {
39-
console.error('Error checking Modrinth:', isOnModrinth);
35+
const modrinthLink = await generateModLink(extractedInfo);
4036

37+
if ('error' in modrinthLink) {
4138
modrinthButton.classList.add('button-modlinker-error');
4239
return;
4340
}
4441

4542
modrinthButton.classList.add(
46-
isOnModrinth ? 'button-modlinker-valid' : 'button-modlinker-warning'
43+
modrinthLink.isOnModrinth
44+
? 'button-modlinker-valid'
45+
: 'button-modlinker-warning'
4746
);
4847

49-
modrinthButton.setAttribute(
50-
'href',
51-
isOnModrinth
52-
? `https://modrinth.com/mod/${extractedInfo.modId}`
53-
: `https://modrinth.com/mods?q=${extractedInfo.modId.replace(
54-
/[-_]/g,
55-
'+'
56-
)}`
57-
);
48+
modrinthButton.setAttribute('href', modrinthLink.link);
5849
})();
5950
});

src/interfaces/searchResultInfo.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1-
export interface SearchResultInfo {
1+
interface ModInfo {
2+
type: 'mod';
23
modId: string;
34
}
5+
6+
interface PluginInfo {
7+
type: 'plugin';
8+
modId: string;
9+
}
10+
11+
export type SearchResultInfo = ModInfo | PluginInfo;

src/lib/extractInfo.ts

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const supportedDomainHandlers: Record<
88
'9minecraft.net': NineMinecraftHandler,
99
'planetminecraft.com': PlanetMinecraftHandler,
1010
'tlauncher.org': TLauncherHandler,
11+
'spigotmc.org': SpigotMCHandler,
12+
'dev.bukkit.org': BukkitHandler,
1113
};
1214

1315
export function ExtractInfo(element: HTMLElement): SearchResultInfo | false {
@@ -29,16 +31,21 @@ export function ExtractInfo(element: HTMLElement): SearchResultInfo | false {
2931
return supportedDomainHandlers[domain](link);
3032
}
3133

34+
// https://www.curseforge.com/minecraft/mc-mods/create
3235
function CurseForgeHandler(link: string): SearchResultInfo | false {
33-
const modId = link.split('/')[5];
34-
if (modId === undefined) return false;
36+
const type = link.split('/')[4];
37+
if (type !== 'mc-mods' && type !== 'bukkit-plugins') return false;
38+
39+
const id = link.split('/')[5];
40+
if (id === undefined) return false;
3541

3642
return {
37-
modId: modId,
43+
type: type === 'mc-mods' ? 'mod' : 'plugin',
44+
modId: id,
3845
};
3946
}
4047

41-
// https://www.9minecraft.net/fabric-api/
48+
// https://www.9minecraft.net/fabric-api
4249
function NineMinecraftHandler(link: string): SearchResultInfo | false {
4350
const splitLink = link.split('/');
4451
if (splitLink.length !== 4) return false;
@@ -47,11 +54,12 @@ function NineMinecraftHandler(link: string): SearchResultInfo | false {
4754
if (modId === undefined) return false;
4855

4956
return {
57+
type: 'mod',
5058
modId: modId,
5159
};
5260
}
5361

54-
// https://www.planetminecraft.com/mods/tag/create/
62+
// https://www.planetminecraft.com/mods/tag/create
5563
function PlanetMinecraftHandler(link: string): SearchResultInfo | false {
5664
if (!link.includes('/mods/tag/')) return false;
5765

@@ -62,6 +70,7 @@ function PlanetMinecraftHandler(link: string): SearchResultInfo | false {
6270
if (modId === undefined) return false;
6371

6472
return {
73+
type: 'mod',
6574
modId: modId,
6675
};
6776
}
@@ -89,6 +98,41 @@ function TLauncherHandler(link: string): SearchResultInfo | false {
8998
}
9099

91100
return {
101+
type: 'mod',
92102
modId: splitModId.join('-'),
93103
};
94104
}
105+
106+
// https://www.spigotmc.org/resources/skinsrestorer.2124
107+
// https://www.spigotmc.org/resources/skinsrestorer.2124/updates
108+
function SpigotMCHandler(link: string): SearchResultInfo | false {
109+
const splitLink = link.split('/');
110+
if (!splitLink.includes('resources')) return false;
111+
112+
const resourcesIndex = splitLink.indexOf('resources');
113+
114+
const modId = splitLink[resourcesIndex + 1].split('.')[0];
115+
if (modId === undefined) return false;
116+
117+
return {
118+
type: 'plugin',
119+
modId: modId,
120+
};
121+
}
122+
123+
// https://dev.bukkit.org/projects/grief-prevention
124+
// https://dev.bukkit.org/projects/grief-prevention/images
125+
function BukkitHandler(link: string): SearchResultInfo | false {
126+
const splitLink = link.split('/');
127+
if (!splitLink.includes('projects')) return false;
128+
129+
const projectsIndex = splitLink.indexOf('projects');
130+
131+
const modId = splitLink[projectsIndex + 1];
132+
if (modId === undefined) return false;
133+
134+
return {
135+
type: 'plugin',
136+
modId: modId,
137+
};
138+
}

src/lib/generateModLink.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { SearchResultInfo } from '../interfaces/searchResultInfo';
2+
3+
const linkMap: Record<SearchResultInfo['type'], string[]> = {
4+
mod: ['https://modrinth.com/mod/', 'https://modrinth.com/mods?q='],
5+
plugin: ['https://modrinth.com/plugin/', 'https://modrinth.com/plugins?q='],
6+
};
7+
8+
export async function generateModLink(info: SearchResultInfo): Promise<
9+
| {
10+
link: string;
11+
isOnModrinth: boolean;
12+
}
13+
| { error: true; errorMessage: string }
14+
> {
15+
const isOnModrinth = await chrome.runtime.sendMessage([
16+
'cacheCheck',
17+
`https://api.modrinth.com/v2/project/${info.modId}`,
18+
]);
19+
20+
if (isOnModrinth instanceof Error) {
21+
console.error('Error checking Modrinth:', isOnModrinth);
22+
return {
23+
error: true,
24+
errorMessage: 'Error checking Modrinth',
25+
};
26+
}
27+
28+
const link = isOnModrinth
29+
? `${linkMap[info.type][0]}${info.modId}`
30+
: `${linkMap[info.type][1]}${info.modId.replace(/[-_]/g, '+')}`;
31+
32+
return {
33+
link,
34+
isOnModrinth,
35+
};
36+
}

0 commit comments

Comments
 (0)