Skip to content

Commit

Permalink
feat: support for Modrinth
Browse files Browse the repository at this point in the history
docs: fix documentation
modified: minor change
perf: some optimizations
fix: change styles
fix: fix bugs
  • Loading branch information
teaSummer committed Jun 28, 2024
1 parent eeccd1c commit d3c3357
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 20 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@

对象有以下属性:

| 属性键名称 | 属性值类型 | 属性值描述 |
|----------|--------|---------------------------------|
| `title` | String | 站点完整名称。 |
| `abbr` | String | 可选。简称,若不填写此属性,将与 `title` 值保持一致。 |
| `search` | String | 搜索后跳转的地址 URL,使用 `<T>` 表示关键词 |
| `note` | String | 可选。备注。 |
| `url` | String | 可选。站点主页地址 URL。 |
| 属性键名称 | 属性值类型 | 属性值描述 |
|----------|--------|---------------------------------------|
| `title` | String | 站点完整名称。 |
| `abbr` | String | 可选。简称,若不填写此属性,将与 `title` 值保持一致。 |
| `search` | String | 搜索后跳转的地址 URL,使用 `<T>` 表示关键词,留空则由前端处理。 |
| `note` | String | 可选。备注。 |
| `url` | String | 可选。站点主页地址 URL。 |

示例如下:

Expand Down
18 changes: 18 additions & 0 deletions assets/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,15 @@ mdui-select {
mdui-menu-item {
padding: 4px 12px;
cursor: pointer;
transition: none;
-webkit-tap-highlight-color: transparent;
}

mdui-menu-item.version-item {
padding: 0 15px;
font-size: 13px;
}

mdui-menu-item[hidden] {
display: none;
}
Expand Down Expand Up @@ -353,10 +359,18 @@ div[id]>*:first-child {
margin-top: 20px;
}

.inline {
display: inline !important;
}

.inline-block {
display: inline-block !important;
}

.gravity-block>* {
display: block;
}

.background {
background-image: url('image/background.jpg');
position: fixed;
Expand Down Expand Up @@ -434,6 +448,10 @@ max-width: calc(min(100vw, 800px) - 4rem);
display: none !important;
}

.acquire-versions {
transition: all 0.1s;
}

.resource-container>div+div,
.device-container+.resource-container {
margin-top: 1rem;
Expand Down
2 changes: 1 addition & 1 deletion data/searchable.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const searchable = [
},
{
title: "Modrinth",
search: "https://modrinth.com/mods?q=<T>",
search: "",
url: "https://modrinth.com"
},
{
Expand Down
29 changes: 26 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="" style="color-scheme: light;">
<html dir="auto" lang="" style="color-scheme: light;">

<head>
<meta charset="UTF-8">
Expand Down Expand Up @@ -101,7 +101,9 @@ <h2 class="page-title" al="app"></h2>
<div class="launcher-title full-name"></div>
<div class="launcher-goto">
<a class="button data-url-launcher" target="_blank" style="display: none;"
al-aplto="title: gotoLauncherWebsite.note"><text al="gotoLauncherWebsite"></text></a>
al-aplto="title: gotoLauncherWebsite.note;">
<text al="gotoLauncherWebsite"></text>
</a>
</div>
<div class="launcher-download by-inline">
<div>
Expand All @@ -124,11 +126,32 @@ <h2 class="page-title" al="website"></h2>
placement="bottom" variant="outlined" required></mdui-select>
</label>
<div class="label searchable-label inline-block"></div>
<div class="searchable-args">
<div class="Modrinth gravity-block" style="display: none;">
<mdui-select class="Modrinth-projectType arg min-width-190" name="Modrinth-projectType"
placement="bottom" variant="outlined" al-aplto="prefix: prefixType;" required>
<mdui-menu-item value="mod" selected><div slot="custom" class="custom-item"><div al="mods"></div></div></mdui-menu-item>
<mdui-menu-item value="plugin"><div slot="custom" class="custom-item"><div al="plugins"></div></div></mdui-menu-item>
<mdui-menu-item value="datapack"><div slot="custom" class="custom-item"><div al="dataPacks"></div></div></mdui-menu-item>
<mdui-menu-item value="shader"><div slot="custom" class="custom-item"><div al="shaders"></div></div></mdui-menu-item>
<mdui-menu-item value="resourcepack"><div slot="custom" class="custom-item"><div al="resourcePacks"></div></div></mdui-menu-item>
<mdui-menu-item value="modpack"><div slot="custom" class="custom-item"><div al="modpacks"></div></div></mdui-menu-item>
</mdui-select>
<mdui-select class="Modrinth-versions arg inline-block" name="Modrinth-versions"
variant="outlined" al-aplto="prefix: prefixVersions; data-value: all;"
multiple required>
<mdui-menu-item value="?"><div slot="custom" class="custom-item"><div al="unknown"></div></div></mdui-menu-item>
<mdui-menu-item value="all" hidden><div slot="custom" class="custom-item"><div al="all"></div></div></mdui-menu-item>
</mdui-select>
<mdui-button style="vertical-align: middle;" class="acquire-versions inline-block" icon="refresh"
al="acquire"></mdui-button>
</div>
</div>
<div class="searchable-search">
<form class="searchable-form">
<label>
<mdui-text-field class="searchable-input" type="text" data-provide="typeahead"
variant="outlined" value="" al-aplto="placeholder: searchFrom">
variant="outlined" al-aplto="placeholder: searchFrom;">
<mdui-button-icon slot="end-icon" icon="search"
class="searchable-button inline-block" type="submit"
disabled></mdui-button-icon>
Expand Down
13 changes: 12 additions & 1 deletion locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,21 @@ data:
searchFrom: ' Search from ${linkSearchFrom} ....'
gotoLauncherWebsite: To launcher website
gotoLauncherWebsite.note: To GitHub Repo or official website
prefixType: 'Type: '
mods: Mods
plugins: Plugins
dataPacks: Data Packs
shaders: Shaders
resourcePacks: Resource Packs
modpacks: Modpacks
prefixVersions: 'Version: '
all: All
acquire: Acquire
unfold: More...
fold: Less
GitHubProxy: >-
Speedup the GitHub with <a href="https://mirror.ghproxy.com" target="_blank">mirror</a> proxy
Speedup the GitHub with <a href="https://mirror.ghproxy.com" target="_blank">mirror proxy</a>
(for Chinese Mainland)
searchableDirect: '{searchable}: Auto access to the detail page when possible'
ellipsis: ....
statistics: Statistics
Expand Down
10 changes: 10 additions & 0 deletions locales/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ data:
searchFrom: ' 从 ${linkSearchFrom} 中搜索 ....'
gotoLauncherWebsite: 跳转启动器网站
gotoLauncherWebsite.note: 跳转至GitHub仓库或官方网站
prefixType: 类型
mods: 模组
plugins: 插件
dataPacks: 数据包
shaders: 光影
resourcePacks: 资源包
modpacks: 整合包
prefixVersions: 版本
all: 全部
acquire: 获取
unfold: 展开...
fold: 收起
GitHubProxy: >-
Expand Down
94 changes: 86 additions & 8 deletions scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ const searchableChanged = function(event = {target: $('.searchable-list')}) {
localStorage.setItem('searchable-checked', event.target.value);
countSearchable += 1;
if (countSearchable > 2) $('.searchable-list').click();
$('.Modrinth').hide();
if (abbr == 'Modrinth') $('.Modrinth').show();
i18n();
};
$('.searchable-list').change(searchableChanged);
Expand All @@ -168,9 +170,23 @@ $('.searchable-form').submit(function(event) {
event.preventDefault();
const input = $('.searchable-input').val().trim();
const search = encodeURI(input);
let url = searchKeyword.replace(encodeURI('<T>'), search);
if ($('.searchable-direct').is(':checked') && url.indexOf('&fulltext=') != -1) {
url = url.replace(/&[^&]*$/, '');
if (searchableAbbr == 'Modrinth') {
const ver = $('.Modrinth-versions').val();
let versions = '';
if (ver != 'all' && ver != '?') {
versions = '&v=';
for (const v of ver) {
versions += `${v}&v=`
};
console.log(versions, ver);
versions = versions.slice(0, -3);
};
url = `https://modrinth.com/${$('.Modrinth-projectType').val()}s?q=${search}${versions}`;
} else {
let url = searchKeyword.replace(encodeURI('<T>'), search);
if ($('.searchable-direct').is(':checked') && url.indexOf('&fulltext=') != -1) {
url = url.replace(/&[^&]*$/, '');
};
};
if (input != '') createSuperLabel(url, 'searchable-search');
});
Expand Down Expand Up @@ -206,6 +222,35 @@ $('.searchable-direct').change(function() {
localStorage.setItem('searchable-direct', $('.searchable-direct').is(':checked'));
});

$('.Modrinth-versions').change(function() {
const val = $(this).val();
if (val.length == 0) $(this).val(['all']);
if (val.length >= 2 && val[val.length - 1] == 'all') $(this).val(val.slice(0, -1));
});

// 获取版本列表
$('.acquire-versions').click(function() {
$(this).attr('loading', true).attr('disabled', true);
const releaseVersions = [];
$.ajax({
url: 'https://piston-meta.mojang.com/mc/game/version_manifest.json',
type: 'get',
cache: true,
dataType: 'json',
success: function(result) {
for (const v of result.versions) if (v.type == 'release') releaseVersions.push(v.id);
let dom = '';
for (const rv of releaseVersions) {
dom += `<mdui-menu-item class="version-item" value="${rv}"><div slot="custom" class="custom-item"><div>${rv}</div></div></mdui-menu-item>`;
};
dom += '<mdui-menu-item value="all" hidden><div slot="custom" class="custom-item"><div al="all"></div></div></mdui-menu-item>';
$('.Modrinth-versions').html(dom);
$('.Modrinth-versions').click();
i18n(() => $('.acquire-versions').removeAttr('loading').attr('disabled', false));
}
}, 0);
});


// 配置初始化
const config = function(settings) {
Expand All @@ -215,7 +260,7 @@ const config = function(settings) {
else $('.' + option).attr('checked', false);
};
};
config({'github-proxy': true, 'searchable-direct': true});
config({'github-proxy': (navigator.language == "zh-CN" ? true : false), 'searchable-direct': true});


// 获取候选词
Expand All @@ -234,10 +279,26 @@ $('.searchable-input').typeahead(
if (!searchableComposition) return;
let abbr = searchableAbbr;
let search = encodeURI($('.searchable-input').val().trim());
let facets;
if (abbr == 'Modrinth') {
let projectType = $('.Modrinth-projectType').val();
const ver = $('.Modrinth-versions').val();
let versions = '';
if (ver == '?') return;
if (ver != 'all') {
versions = ',["versions:';
for (const v of ver) {
versions += `${v}","versions:`
};
versions = versions.slice(0, -12) + '"]';
};
facets = encodeURI(`[["categories:'forge'","categories:'fabric'","categories:'quilt'","categories:'liteloader'","categories:'modloader'","categories:'rift'","categories:'neoforge'"],["project_type:${projectType}"]${versions}]`);
};
// API
const api = {
Wiki: `https://zh.minecraft.wiki/api.php?action=opensearch&search=${search}&limit=11`,
BWiki: `https://wiki.biligame.com/mc/api.php?action=opensearch&search=${search}&limit=11`,
Modrinth: `https://api.modrinth.com/v2/search?limit=11&index=relevance&query=${search}&facets=${facets}`,
BEDW: `https://wiki.mcbe-dev.net/w/api.php?action=opensearch&search=${search}&namespace=0%7C3000%7C3002%7C3004%7C3008%7C3010&limit=11`,
MinePlugin: `https://mineplugin.org/api.php?action=opensearch&search=${search}&limit=11`
};
Expand All @@ -249,20 +310,29 @@ $('.searchable-input').typeahead(
url: url,
type: 'get',
cache: true,
data: {keyword: query},
dataType: 'jsonp',
dataType: (() => {
if (abbr == 'Modrinth') return 'json';
else return 'jsonp';
})(),
success: function(result) {
if (result.length > 1 && Array.isArray(result[1])) {
result = result[1];
};
if (abbr == 'Modrinth') {
let arr = [];
result.hits.map(function(item) {
arr.push(item.title);
});
result = arr;
};
$('.searchable-clear').removeAttr('loading'); // 在获取候选词时显示加载动画(已完成)
return asyncResults(result);
},
error: function() {
error: function(a) {
$('.searchable-clear').removeAttr('loading'); // 在获取候选词时显示加载动画(已完成)
}
}, 0);
};
}
});
}
}
Expand Down Expand Up @@ -375,12 +445,20 @@ $(document).ready(function() {
$('.pre-flex').each(function(i, e) {
pre_list(e);
});
$('.searchable-args .arg:not([multiple])').each(function(i, e) {
$(e).children().click(function() {
$(e).click();
});
});
// 国际化 (internationalization) 准备进行
al.setDefaultCountry({
en: 'en',
zh: 'zh-CN'
});
i18n(function() {
// 默认值初始化
$('.Modrinth-projectType').val('mod');
$('.Modrinth-versions').val(['all']);
// 最后处理
hashChanged();
$('.wait').removeAttr('class').removeAttr('style');
Expand Down

0 comments on commit d3c3357

Please sign in to comment.