Adds web search functionality

This commit is contained in:
Alicia Sykes 2021-08-28 15:55:41 +01:00
parent 5db0909e37
commit 411357c5b6
2 changed files with 68 additions and 2 deletions

View File

@ -1,5 +1,5 @@
<template>
<form>
<form @submit.prevent="searchSubmitted">
<label for="filter-tiles">{{ $t('search.search-label') }}</label>
<input
id="filter-tiles"
@ -16,12 +16,15 @@
</template>
<script>
import router from '@/router';
import ArrowKeyNavigation from '@/utils/ArrowKeyNavigation';
import ErrorHandler from '@/utils/ErrorHandler';
import { getCustomKeyShortcuts } from '@/utils/ConfigHelpers';
import { searchEngineUrls, defaultSearchEngine, defaultSearchOpeningMethod } from '@/utils/defaults';
export default {
name: 'FilterTile',
inject: ['config'],
props: {
active: Boolean,
},
@ -74,6 +77,40 @@ export default {
}
});
},
launchWebSearch(url, method) {
switch (method) {
case 'newtab':
window.open(url, '_blank');
break;
case 'sametab':
window.open(url, '_self');
break;
case 'workspace':
router.push({ name: 'workspace', query: { url } });
break;
default:
ErrorHandler(`Unknown opening method: ${method}`);
window.open(url, '_blank');
}
},
searchSubmitted() {
// Get search preferences from appConfig
const { appConfig } = this.config;
const searchPrefs = appConfig.webSearch || {};
if (!searchPrefs.disableWebSearch) { // Only proceed if user hasn't disabled web search
const openingMethod = searchPrefs.openingMethod || defaultSearchOpeningMethod;
// Get search engine, and make URL
const searchEngine = searchPrefs.searchEngine || defaultSearchEngine;
let searchUrl = searchEngineUrls[searchEngine];
if (!searchUrl) ErrorHandler(`Search engine not found - ${searchEngine}`);
if (searchEngine === 'custom' && searchPrefs.customSearchEngine) {
searchUrl = searchPrefs.customSearchEngine;
}
// Append users encoded query onto search URL, and launch
searchUrl += encodeURIComponent(this.input);
this.launchWebSearch(searchUrl, openingMethod);
}
},
},
};
</script>

View File

@ -152,6 +152,35 @@ module.exports = {
localPath: '/item-icons',
faviconName: 'favicon.ico',
},
/* URLs for web search engines */
searchEngineUrls: {
// Common
duckduckgo: 'https://duckduckgo.com/?q=',
google: 'https://google.com/search?q=',
whoogle: 'https://whoogle.sdf.org/search?q=',
qwant: 'https://www.qwant.com/?q=',
startpage: 'https://www.startpage.com/do/search?query=',
// Niche
'searx-bar': 'https://searx.bar/search?q=',
'searx-info': 'https://searx.info/search?q=',
'searx-tiekoetter': 'https://searx.tiekoetter.com/search?q=',
'searx-bissisoft': 'https://searx.bissisoft.com/search?q=',
ecosia: 'https://www.ecosia.org/search?q=',
metager: 'https://metager.org/meta/meta.ger3?eingabe=',
swisscows: 'https://swisscows.com/web?query=',
mojeek: 'https://www.mojeek.com/search?q=',
peekier: 'https://peekier.com/#!',
// Specific
wikipedia: 'https://en.wikipedia.org/w/?search=',
stackoverflow: 'https://stackoverflow.com/search?q=',
wolframalpha: 'https://www.wolframalpha.com/input/?i=',
reddit: 'https://www.reddit.com/search/?q=',
youtube: 'https://youtube.com/results?q=',
github: 'https://github.com/search?q=',
bbc: 'https://www.bbc.co.uk/search?q=',
},
defaultSearchEngine: 'duckduckgo',
defaultSearchOpeningMethod: 'newtab',
/* Available built-in colors for the theme builder */
swatches: [
['#eb5cad', '#985ceb', '#5346f3', '#5c90eb'],