Implement OpenSearch support

This commit is contained in:
Newbyte 2020-08-07 18:17:29 +02:00 committed by Sean Yesmunt
parent abbaa8bf78
commit 2525dcf983
5 changed files with 34 additions and 1 deletions

View file

@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Added
- Block mature content when accessed directly from URL _community pr!_ ([#4560](https://github.com/lbryio/lbry-desktop/pull/4560))
- You can now add LBRY as a search engine in your browser (via OpenSearch) _community pr!_ ([#4640](https://github.com/lbryio/lbry-desktop/pull/4640))
### Changed

4
static/opensearch.xml Normal file
View file

@ -0,0 +1,4 @@
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">
<!-- VARIABLE_XML_BEGIN -->
<!-- VARIABLE_XML_END -->
</OpenSearchDescription>

View file

@ -59,7 +59,9 @@ function buildOgMetadata(overrideOptions = {}) {
`<meta name="twitter:image" content="${OG_IMAGE_URL || `${URL}/public/v2-og.png`}"/>\n` +
`<meta name="twitter:url" content="${URL}" />\n` +
'<meta property="fb:app_id" content="1673146449633983" />\n' +
`<link rel="canonical" content="${SITE_CANONICAL_URL || URL}"/>`;
`<link rel="canonical" content="${SITE_CANONICAL_URL || URL}"/>` +
`<link rel="search" type="application/opensearchdescription+xml" title="${SITE_NAME ||
SITE_TITLE}" href="${URL}/opensearch.xml">`;
return head;
}

18
web/src/xml.js Normal file
View file

@ -0,0 +1,18 @@
const { URL, SITE_TITLE } = require('../../config.js');
function getOpenSearchXml() {
return (
`<ShortName>${SITE_TITLE}</ShortName>` +
`<Description>Search ${SITE_TITLE}</Description>` +
'<InputEncoding>UTF-8</InputEncoding>' +
`<Image width="32" height="32" type="image/png">${URL}/public/favicon.png</Image>` +
`<Url type="text/html" method="get" template="${URL}/$/search?q={searchTerms}"/>` +
`<moz:SearchForm>${URL}</moz:SearchForm>`
);
}
function insertVariableXml(fullXml, xmlToInsert) {
return fullXml.replace(/<!-- VARIABLE_XML_BEGIN -->.*<!-- VARIABLE_XML_END -->/s, xmlToInsert);
}
module.exports = { getOpenSearchXml, insertVariableXml };

View file

@ -7,6 +7,7 @@ const CopyWebpackPlugin = require('copy-webpack-plugin');
const { DefinePlugin, ProvidePlugin } = require('webpack');
const SentryWebpackPlugin = require('@sentry/webpack-plugin');
const { insertToHead, buildBasicOgMetadata } = require('./src/html');
const { insertVariableXml, getOpenSearchXml } = require('./src/xml');
const CUSTOM_ROOT = path.resolve(__dirname, '../custom/');
const STATIC_ROOT = path.resolve(__dirname, '../static/');
@ -24,6 +25,13 @@ const copyWebpackCommands = [
return insertToHead(content.toString(), buildBasicOgMetadata());
},
},
{
from: `${STATIC_ROOT}/opensearch.xml`,
to: `${DIST_ROOT}/opensearch.xml`,
transform(content, path) {
return insertVariableXml(content.toString(), getOpenSearchXml());
},
},
{
from: `${STATIC_ROOT}/img/favicon.png`,
to: `${DIST_ROOT}/public/favicon.png`,