From a7263715255207d209a617a2b38a21131c6eed15 Mon Sep 17 00:00:00 2001 From: "H. Kamran" Date: Tue, 26 Oct 2021 20:43:25 +0200 Subject: [PATCH] :adhesive_bandage: Add the Web Share Target API (#46) --- client/pages/index.vue | 28 ++++++++++++++++++++++++++++ nuxt.config.js | 11 ++++++++++- package.json | 2 +- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/client/pages/index.vue b/client/pages/index.vue index 5715e6d..33f07f5 100644 --- a/client/pages/index.vue +++ b/client/pages/index.vue @@ -71,6 +71,34 @@ export default { } else if (link) { $modal.replace('linkDetails', { link }) } + + // Web Share Target API + const { searchParams } = new URL(window.location) + let sharedUrl = '' + + const validateUrl = (string) => { + let url + + try { + url = new URL(string) + } catch (_) { + return false + } + + return url.protocol === 'http:' || url.protocol === 'https:' + } + + if (searchParams.get('url') && validateUrl(searchParams.get('url'))) { + sharedUrl = searchParams.get('url') + } else if (searchParams.get('text') && validateUrl(searchParams.get('text'))) { + sharedUrl = searchParams.get('text') + } else if (searchParams.get('title') && validateUrl(searchParams.get('title'))) { + sharedUrl = searchParams.get('title') + } + + if (sharedUrl) { + $modal.replace('addLink', { inputValue: sharedUrl }) + } }, data() { return { diff --git a/nuxt.config.js b/nuxt.config.js index aa64329..c9ad6e0 100644 --- a/nuxt.config.js +++ b/nuxt.config.js @@ -41,7 +41,16 @@ export default { name: 'WebCrate App', short_name: 'WebCrate', description: `WebCrate helps you organize and share links, articles and more from around the web. It's more than just a bookmarking tool.`, - theme_color: '#F7F6F4' + theme_color: '#F7F6F4', + share_target: { + action: '/', + method: 'GET', + params: { + title: 'title', + text: 'text', + url: 'url' + } + } } }, diff --git a/package.json b/package.json index d5baa96..35e4db6 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "build:server": "tsc -p ./server/tsconfig.json", "lint:nuxt": "eslint --ext \".js,.vue\" --ignore-path .gitignore .", "lint:server": "eslint ./server/ --ext .ts", - "copy-templates": "cp ./server/views ./build/views -r && cp ./server/service/image/NotoColorEmoji.ttf ./build/service/image", + "copy-templates": "cp -R ./server/views ./build/views && cp ./server/service/image/NotoColorEmoji.ttf ./build/service/image", "clean": "rimraf dist build .nuxt", "postbuild:server": "npm run copy-templates", "prebuild": "npm run clean",