35 lines
1.1 KiB
JavaScript
35 lines
1.1 KiB
JavaScript
/*! Copyright (C) 2018 Charles de Lacombe
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
function search(query) {
|
|
return new Promise((resolve, reject) => {
|
|
fetch(`https://peertube.video/api/v1/videos/search?search=${encodeURIComponent(query)}`)
|
|
.then(res => res.json())
|
|
.then(function(data) {
|
|
if (data.total > 0) {
|
|
resolve(data.data[0]);
|
|
} else {
|
|
reject(new Error('No results.'))
|
|
}
|
|
})
|
|
});
|
|
};
|
|
|
|
|
|
browser.runtime.onMessage.addListener(function(message, sender) {
|
|
return search(message.query)
|
|
});
|