Class: ShareTarget

ShareTarget(config)

A class to allow custom share targets to the polyfill of navigator.share() These just work off of opening constructed url in new tab for now todo: file download/share suport todo: refien options and enable custom options todo: allow ShareTarget defaults

Constructor

new ShareTarget(config)

https://web.dev/web-share-target/ Register a possible url if the one you want is not provided url = origin + action + params.join('&'); I try to deal with the / and ? properly
Parameters:
Name Type Description
config This is the config like in the manifest but with a few extra options
Properties
Name Type Attributes Default Description
name String the targetName can be used with shareTo
niceName String <optional>
config.name UNOFFICIAL human frendly name for the UI
origin String UNOFFICIAL this is the extra pesice of info not in the manifest that we need for a share_target
icon String UNOFFICIAL IMPORTANT MUST HAVE class='the-icon' in svg tag; todo add suport for using manifest icons this is simply an svg pasted in
canShare function <optional>
todo a function that takes (data, options) passed in to navigator.share and tells us if it can use this data. note the data is passed
share_target String the defonition of the share url and its required paramater
Properties
Name Type Attributes Default Description
action Object The URI placed after the orgin ie '/share/
method String <optional>
"GET" the http methof for the fetch
enctype String <optional>
"multipart/form-data" the encoding type to send https://web.dev/web-share-target/#accepting-files
params Object Each parameter of {@see ShareTarget.share} can be mapped to a url search parameter of choice ie url: 'the_share_url' => /share?the_share_url=${url} Theoretically you can add any arg you want but you must provide it when you call share(data) in case the user selects the target that needs it. you should make your target flexible to missing args
Properties
Name Type Attributes Default Description
title String the Header of the share link defaults to url if not provided
text String <optional>
the main body of the share message defaults to title : desc if not provided
url String the url
files String <optional>
todo files suport
Properties
Name Type Attributes Description
name String <optional>
accept String <optional>
desc String <optional>
UNOFFICIAL a description of what is being shared if text is provided and not desc text is used for desc NOTE YOU CAN ONLY USE UNOFFICIAL PARAMS WITH sharePolyfill.share() not navigator.share = sharePolyfill.share if your app depends on these and you dont want to use native navigator.share ever
image String <optional>
UNOFFICIAL an image url for the share card
app_id String <optional>
UNOFFICIAL an app id/ token
redirect String <optional>
UNOFFICIAL a redirect url for when the shareTarget is done
hashtags String <optional>
UNOFFICIAL a array of labels for use in twitter like hashtags
language String <optional>
'en' UNOFFICIAL for defining the language the share_target should disply in
provider String <optional>
window.location.host UNOFFICIAL
via String <optional>
window.location.href UNOFFICIAL the referring host/ thing default to window.location.href
category String <optional>
UNOFFICIAL what category to submit this share into
payload String <optional>
'title : url\n desc \n tags' this is a useful glob of the args for use in copy etc
user_id String <optional>
UNOFFICIAL the user id of the person sharing
phone_number String <optional>
UNOFFICIAL the phone numbers to share to if we know used for sms
email_address String <optional>
UNOFFICIAL the email address to share to (used for gmail/yahoo etc)
cc_email_address String <optional>
UNOFFICIAL bcc emails comma seperated usually used with email
bcc_email_address String <optional>
UNOFFICIAL cc emails comma seperated usually used with email
execute function <optional>
If desired you may override the actual execute function all data will be passed just before execution for you to do with as you please
defaults Object <optional>
this lets you set defult paramaters, this is recommended for all unofficial paramaters needed as we should not asume the end user will pass them in. For example we could pass in the app_id when registering so we do not have to pass it in every time we call navigator.share()
Source:
Example
// 'telegram.me': 'https://t.me/share/url?url=' + url + '&text=' + text + '&to=' + phone_number,
   sharePolyfill.registerShareTarget(new ShareTarget({

     name: 'telegram',
     niceName: "Telagram",
     origin: 'https://t.me',

     icon: '<svg class="the-icon" width="31" height="32" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 496 512"><path fill="#0088cc" d="M248 8C111 8 0 119 0 256s111 248 248 248 248-111 248-248S385 8 248 8zm121.8 169.9l-40.7 191.8c-3 13.6-11.1 16.9-22.4 10.5l-62-45.7-29.9 28.8c-3.3 3.3-6.1 6.1-12.5 6.1l4.4-63.1 114.9-103.8c5-4.4-1.1-6.9-7.7-2.5l-142 89.4-61.2-19.1c-13.3-4.2-13.6-13.3 2.8-19.7l239.1-92.2c11.1-4 20.8 2.7 17.2 19.5z"></path></svg>',

     share_target: {
       action: 'share/url?',
       method: 'get',
       params: {
         url: 'url',
         text: 'text',
         phone_number: 'phone_number',
         // hash_tags: 'hashtags'
       }
     }
   }))

sharePolyfill.re

Methods

share(data)

Execute a share action on this target this will perform the fetch in accordance to the config or simply execute the function forwarding data
Parameters:
Name Type Description
data See constructor for template arg info
Source: