121 行
3.9 KiB
JavaScript
121 行
3.9 KiB
JavaScript
|
|
|
|
// home.js
|
|
|
|
// GitHub
|
|
(function updateGitHubBadges() {
|
|
if (!document.hidden) {
|
|
let repository = $('meta[name=repository]').attr('content');
|
|
$('#github-star img').attr('src', `https://img.shields.io/github/stars/${repository}?style=social`);
|
|
$('#github-fork img').attr('src', `https://img.shields.io/github/forks/${repository}?style=social`);
|
|
};
|
|
setTimeout(function () {
|
|
updateGitHubBadges();
|
|
}, 1000);
|
|
})();
|
|
|
|
// build time
|
|
dayjs.locale('zh-cn');
|
|
dayjs.extend(window.dayjs_plugin_relativeTime);
|
|
(function updateBuildTimeBadge() {
|
|
if (!document.hidden) {
|
|
$('#build-time img').attr('src', `https://img.shields.io/badge/%E6%9E%84%E5%BB%BA%E4%BA%8E-${encodeURIComponent(dayjs($('meta[name=built_at]').attr('content')).fromNow())}-brightgreen?logo=jekyll`);
|
|
};
|
|
setTimeout(function () {
|
|
updateBuildTimeBadge();
|
|
}, 1000);
|
|
})();
|
|
|
|
|
|
|
|
|
|
function setCookie(name, value) {
|
|
Cookies.set(name, value, {
|
|
expires: 365,
|
|
domain: '.byr-navi.com',
|
|
secure: true
|
|
});
|
|
};
|
|
|
|
function redirect(service, query) {
|
|
if (service.data('transcode')) {
|
|
query = query.replace(new RegExp(service.data('transcode-from'), 'g'), service.data('transcode-to'));
|
|
};
|
|
window.open(`search/?service=${encodeURIComponent(service.text())}&query=${query}&next=${encodeURIComponent(service.data('url').replace('{query}', query))}`, '_blank');
|
|
};
|
|
|
|
// initialize previous used search service
|
|
if (Cookies.get('byr_navi_previous_used_search_service') === undefined || Cookies.get('byr_navi_previous_used_search_service') === '' || $(`#${Cookies.get('byr_navi_previous_used_search_service')}`).length === 0) {
|
|
setCookie('byr_navi_previous_used_search_service', $('#search-services').val());
|
|
} else {
|
|
updateDropdown();
|
|
};
|
|
|
|
// search button
|
|
$('#search-button').click(function () {
|
|
let service = $(`#${$('#search-services').val()}`);
|
|
let query = $('#search-query').val();
|
|
query = encodeURIComponent(query);
|
|
if (query) {
|
|
setCookie('byr_navi_previous_used_search_service', service.val());
|
|
redirect(service, query);
|
|
} else {
|
|
$('#search-div').addClass('error');
|
|
$('#search-query').attr('placeholder', '请输入搜索内容');
|
|
};
|
|
});
|
|
|
|
// query input: click to select
|
|
$('#search-query').click(function () {
|
|
$(this).select();
|
|
});
|
|
|
|
|
|
// query input: press return/enter to submit
|
|
$(window).keyup(function (event) {
|
|
let windowTop = $(window).scrollTop();
|
|
let windowHeight = $(window).innerHeight();
|
|
let windowBottom = windowTop + windowHeight;
|
|
let searchBoxTop = $('#search-div').offset().top;
|
|
let searchBoxHeight = $('#search-div').innerHeight();
|
|
let searchBoxBottom = searchBoxTop + searchBoxHeight;
|
|
if (event.key === 'Enter' && searchBoxBottom > windowTop && searchBoxTop < windowBottom) {
|
|
let service = $(`#${$('#search-services').val()}`);
|
|
let query = $('#search-query').val();
|
|
query = encodeURIComponent(query);
|
|
if (query) {
|
|
if ($('#search-query:focus').length > 0) {
|
|
setCookie('byr_navi_previous_used_search_service', service.val());
|
|
redirect(service, query);
|
|
} else {
|
|
$('#search-query').focus().select();
|
|
};
|
|
} else {
|
|
$('#search-div').addClass('error');
|
|
$('#search-query').attr('placeholder', '请输入搜索内容').focus();
|
|
};
|
|
};
|
|
});
|
|
|
|
|
|
|
|
// initialize customized shortcuts
|
|
if (Cookies.get('byr_navi_search_shortcuts')) {
|
|
let shortcuts = JSON.parse(Cookies.get('byr_navi_search_shortcuts'));
|
|
$('#search-shortcuts .ui.label').each(function () {
|
|
if (shortcuts[$(this).data('search-service-id')]) {
|
|
if ($(this).hasClass('hidden')) {
|
|
$(this).removeClass('hidden');
|
|
};
|
|
} else {
|
|
if (!$(this).hasClass('hidden')) {
|
|
$(this).addClass('hidden');
|
|
};
|
|
};
|
|
});
|
|
};
|
|
|
|
|
|
|
|
|