2017-06-01 14:18:06 +00:00
|
|
|
/*
|
2017-06-01 16:29:40 +00:00
|
|
|
Copyright 2016 Aviral Dasgupta
|
2017-06-01 14:18:06 +00:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2018-06-19 11:06:13 +00:00
|
|
|
Copyright 2017, 2018 New Vector Ltd
|
2017-06-01 14:18:06 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-04-21 09:01:05 +00:00
|
|
|
import React from 'react';
|
2017-06-01 14:18:06 +00:00
|
|
|
import { _t } from '../languageHandler';
|
2016-06-12 11:32:46 +00:00
|
|
|
import AutocompleteProvider from './AutocompleteProvider';
|
|
|
|
|
2016-07-03 16:45:13 +00:00
|
|
|
import {TextualCompletion} from './Components';
|
2020-04-20 18:00:54 +00:00
|
|
|
import {ICompletion, ISelectionRange} from "./Autocompleter";
|
2016-07-03 16:45:13 +00:00
|
|
|
|
2016-06-21 10:16:20 +00:00
|
|
|
const DDG_REGEX = /\/ddg\s+(.+)$/g;
|
2016-07-03 16:45:13 +00:00
|
|
|
const REFERRER = 'vector';
|
2016-06-12 11:32:46 +00:00
|
|
|
|
|
|
|
export default class DuckDuckGoProvider extends AutocompleteProvider {
|
2016-06-21 10:16:20 +00:00
|
|
|
constructor() {
|
|
|
|
super(DDG_REGEX);
|
|
|
|
}
|
2017-01-20 14:22:27 +00:00
|
|
|
|
2020-04-20 18:00:54 +00:00
|
|
|
static getQueryUri(query: string) {
|
2016-07-04 20:07:01 +00:00
|
|
|
return `https://api.duckduckgo.com/?q=${encodeURIComponent(query)}`
|
2016-07-03 16:45:13 +00:00
|
|
|
+ `&format=json&no_redirect=1&no_html=1&t=${encodeURIComponent(REFERRER)}`;
|
2016-06-12 11:32:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 18:00:54 +00:00
|
|
|
async getCompletions(query: string, selection: ISelectionRange, force= false): Promise<ICompletion[]> {
|
2017-10-11 16:56:17 +00:00
|
|
|
const {command, range} = this.getCurrentCommand(query, selection);
|
2016-07-03 16:45:13 +00:00
|
|
|
if (!query || !command) {
|
2016-09-13 10:11:52 +00:00
|
|
|
return [];
|
2016-07-03 16:45:13 +00:00
|
|
|
}
|
2016-06-12 11:32:46 +00:00
|
|
|
|
2016-09-13 10:11:52 +00:00
|
|
|
const response = await fetch(DuckDuckGoProvider.getQueryUri(command[1]), {
|
2016-07-03 16:45:13 +00:00
|
|
|
method: 'GET',
|
2016-09-13 10:11:52 +00:00
|
|
|
});
|
|
|
|
const json = await response.json();
|
2017-10-11 16:56:17 +00:00
|
|
|
const results = json.Results.map((result) => {
|
2016-09-13 10:11:52 +00:00
|
|
|
return {
|
|
|
|
completion: result.Text,
|
|
|
|
component: (
|
|
|
|
<TextualCompletion
|
|
|
|
title={result.Text}
|
|
|
|
description={result.Result} />
|
|
|
|
),
|
|
|
|
range,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
if (json.Answer) {
|
|
|
|
results.unshift({
|
|
|
|
completion: json.Answer,
|
|
|
|
component: (
|
|
|
|
<TextualCompletion
|
|
|
|
title={json.Answer}
|
|
|
|
description={json.AnswerType} />
|
|
|
|
),
|
|
|
|
range,
|
2016-06-12 11:32:46 +00:00
|
|
|
});
|
2016-09-13 10:11:52 +00:00
|
|
|
}
|
|
|
|
if (json.RelatedTopics && json.RelatedTopics.length > 0) {
|
|
|
|
results.unshift({
|
|
|
|
completion: json.RelatedTopics[0].Text,
|
|
|
|
component: (
|
|
|
|
<TextualCompletion
|
|
|
|
title={json.RelatedTopics[0].Text} />
|
|
|
|
),
|
|
|
|
range,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (json.AbstractText) {
|
|
|
|
results.unshift({
|
|
|
|
completion: json.AbstractText,
|
|
|
|
component: (
|
|
|
|
<TextualCompletion
|
|
|
|
title={json.AbstractText} />
|
|
|
|
),
|
|
|
|
range,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return results;
|
2016-06-12 11:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getName() {
|
2017-06-01 14:18:06 +00:00
|
|
|
return '🔍 ' + _t('Results from DuckDuckGo');
|
2016-06-12 11:32:46 +00:00
|
|
|
}
|
2016-06-20 08:22:55 +00:00
|
|
|
|
2020-04-20 18:00:54 +00:00
|
|
|
renderCompletions(completions: React.ReactNode[]): React.ReactNode {
|
2019-10-14 09:37:10 +00:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className="mx_Autocomplete_Completion_container_block"
|
|
|
|
role="listbox"
|
|
|
|
aria-label={_t("DuckDuckGo Results")}
|
|
|
|
>
|
|
|
|
{ completions }
|
|
|
|
</div>
|
|
|
|
);
|
2016-08-22 19:06:31 +00:00
|
|
|
}
|
2016-06-12 11:32:46 +00:00
|
|
|
}
|