UNPKG

web-sharing

Version:

Web Content Sharing JavaScript Package for the Web.

87 lines (86 loc) 3.45 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TypeOfSharing = exports.WebSharingError = void 0; class WebSharingError extends Error { constructor(...params) { super(...params); this.name = "WebSharingError"; } } exports.WebSharingError = WebSharingError; var TypeOfSharing; (function (TypeOfSharing) { TypeOfSharing["COPY"] = "copy"; TypeOfSharing["APP"] = "app"; })(TypeOfSharing || (TypeOfSharing = {})); exports.TypeOfSharing = TypeOfSharing; const copyURL = (share) => __awaiter(void 0, void 0, void 0, function* () { // The code comment below does not work because Kakao Talk browser does not support it // await navigator.clipboard.writeText(HOMEPAGE_URL) const vmTextArea = document.createElement("textarea"); document.body.appendChild(vmTextArea); vmTextArea.style.top = "0"; vmTextArea.style.left = "0"; vmTextArea.style.position = "fixed"; vmTextArea.style.zIndex = "-1"; vmTextArea.style.color = "white"; vmTextArea.value = share.copyValue || share.url; vmTextArea.select(); document.execCommand("copy"); document.body.removeChild(vmTextArea); }); const shareUsingTheApp = (share) => __awaiter(void 0, void 0, void 0, function* () { yield navigator.share({ title: share.title, text: share.text, url: share.url }); }); const validate = (share) => { if (location.protocol === "http:") { console.warn("App sharing on the Web is only available in the https protocol."); } if (share === undefined || share.title === undefined || share.url === undefined) { throw new WebSharingError("The share, share.title and share.url parameter is a required input value. Please refer to the README.md file."); } }; const shareWebPage = (share, onSuccess, onFail) => { try { validate(share); const isEnableNavigator = navigator.share; if (isEnableNavigator) { shareUsingTheApp(share) .then(() => onSuccess(TypeOfSharing.APP)) .catch((e) => { if (!String(e).includes("canceled")) { onFail(e); } }); } else { copyURL(share) .then(() => onSuccess(TypeOfSharing.COPY)) .catch(onFail); } } catch (e) { if (e instanceof ReferenceError && (e.message == "navigator is not defined" || e.message == "location is not defined")) { throw new WebSharingError("The web-sharing package is only available on the web."); } if (onFail !== undefined) { return onFail(e); } throw e; } }; exports.default = shareWebPage;