From 0cf6a623c768ba2c81e5e3dc90e170ba8b080e14 Mon Sep 17 00:00:00 2001 From: Massimo Artizzu Date: Wed, 24 Jul 2019 00:53:46 +0200 Subject: [PATCH] Restored LinkedIn sharer with limited functionality (fixes #37) --- changelog.md | 8 ++- dist/sharers/linked-in.js | 2 + dist/sharers/linked-in.js.map | 1 + readme.md | 12 ++++ src/sharers/linked-in.js | 37 +++++++++++++ test/sharers/linked-in.js | 100 ++++++++++++++++++++++++++++++++++ 6 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 dist/sharers/linked-in.js create mode 100644 dist/sharers/linked-in.js.map create mode 100644 src/sharers/linked-in.js create mode 100644 test/sharers/linked-in.js diff --git a/changelog.md b/changelog.md index c3e48db..cb3e792 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,7 @@ +# 1.3.1 + +* Restored LinkedIn sharer with limited functionality (issue #37) + # 1.3.0 * Refactored build process, added sourcemaps (issue #32) @@ -7,8 +11,8 @@ # 1.2.2 -* Removed `target="_blank"` to email sharer (issue #13) -* Added `!default` modifier to SCSS variable definitions (issue #14) +* Removed `target="_blank"` to email sharer (issue #12) +* Added `!default` modifier to SCSS variable definitions (issue #13) # 1.2.1 diff --git a/dist/sharers/linked-in.js b/dist/sharers/linked-in.js new file mode 100644 index 0000000..d419a0f --- /dev/null +++ b/dist/sharers/linked-in.js @@ -0,0 +1,2 @@ +!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e=e||self).ShareThisViaLinkedIn={})}(this,function(e){"use strict";var n=!1;e.action=function(e,n){e.preventDefault(),n.ownerDocument.defaultView.open(n.firstChild.href,"share_via_linked_in","height=440,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,width=640").opener=null},e.getShareUrl=function(e){return"https://www.linkedin.com/sharing/share-offsite/?url="+encodeURIComponent(e)},e.name="linked-in",e.render=function(e,o,t){return n||(n=!0,console.warn("LinkedIn doesn't allow sharing links with custom titles anymore, so the main point of ShareThis (sharing a portion of text) couldn't be accomplished. You're encouraged to share your URLs with other, more conventional means, like the official LinkedIn share plugin. See https://docs.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/plugins/share-plugin")),''},Object.defineProperty(e,"__esModule",{value:!0})}); +//# sourceMappingURL=linked-in.js.map diff --git a/dist/sharers/linked-in.js.map b/dist/sharers/linked-in.js.map new file mode 100644 index 0000000..076795f --- /dev/null +++ b/dist/sharers/linked-in.js.map @@ -0,0 +1 @@ +{"version":3,"file":"linked-in.js","sources":["../../src/sharers/linked-in.js"],"sourcesContent":["let shownWarning = false;\n\nexport function render(_text, _rawText, refUrl) {\n if (!shownWarning) {\n shownWarning = true;\n console.warn(\"LinkedIn doesn't allow sharing links with custom titles anymore, so the main point of ShareThis \"\n + \"(sharing a portion of text) couldn't be accomplished. You're encouraged to share your URLs with other, \"\n + \"more conventional means, like the official LinkedIn share plugin. See \"\n + \"https://docs.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/plugins/share-plugin\");\n }\n const url = this.getShareUrl(refUrl);\n\n return ``\n + \"\"\n + \"\"\n + \"\";\n}\n\nexport function getShareUrl(refUrl) {\n return `https://www.linkedin.com/sharing/share-offsite/?url=${encodeURIComponent(refUrl)}`;\n}\n\nexport function action(event, item) {\n event.preventDefault();\n const popup = item.ownerDocument.defaultView.open(\n item.firstChild.href,\n \"share_via_linked_in\",\n \"height=440,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,width=640\"\n );\n popup.opener = null;\n}\n\nexport const name = \"linked-in\";\n"],"names":["shownWarning","event","item","preventDefault","ownerDocument","defaultView","open","firstChild","href","opener","refUrl","encodeURIComponent","_text","_rawText","console","warn","this","getShareUrl"],"mappings":"iNAAA,IAAIA,GAAe,WA0BZ,SAAgBC,EAAOC,GAC1BD,EAAME,iBACQD,EAAKE,cAAcC,YAAYC,KACzCJ,EAAKK,WAAWC,KAChB,sBACA,kFAEEC,OAAS,oBAXZ,SAAqBC,gEACsCC,mBAAmBD,WAajE,qBAlCb,SAAgBE,EAAOC,EAAUH,UAC/BV,IACDA,GAAe,EACfc,QAAQC,KAAK,iXAOV,YAFKC,KAAKC,YAAYP"} \ No newline at end of file diff --git a/readme.md b/readme.md index 3e1a16d..be1ff48 100644 --- a/readme.md +++ b/readme.md @@ -175,9 +175,19 @@ Twitter | `dist/sharers/twitter.js` | `twitter` | `ShareThisViaTwitter` Facebook | `dist/sharers/facebook.js` | `facebook` | `ShareThisViaFacebook` Reddit | `dist/sharers/reddit.js` | `reddit` | `ShareThisViaReddit` Email | `dist/sharers/email.js` | `email` | `ShareThisViaEmail` +LinkedIn (*see note below!) | `dist/sharers/linked-in.js` | `linked-in` | `ShareThisViaLinkedIn` You can find a couple more on the [presentation page](https://maxart2501.github.io/share-this/) of the library. +**Note about the LinkedIn sharer**: LinkedIn doesn't allow sharing a site with a custom title/snippet of text. Therefore ShareThis would fail +to bring any value relatively to other sharing methods. The sharer is kept for backwards compatibility only. The following warning will appear +in the console the first time the sharer is rendered: + +> LinkedIn doesn't allow sharing links with custom titles anymore, so the main point of ShareThis +> (sharing a portion of text) couldn't be accomplished. You're encouraged to share your URLs with other, +> more conventional means, like the official LinkedIn share plugin. See +> https://docs.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/plugins/share-plugin + ## Developer friendly @@ -228,8 +238,10 @@ voice synthesis"), so you might want to show *both* native and custom sharing in ## To do +* Name change ([issue](https://github.com/MaxArt2501/share-this/issues/38)) * More test coverage * Support for JSX in sharers' `render` method +* Move the sharers from the library to separate packages ([issue](https://github.com/MaxArt2501/share-this/issues/39)) ## License diff --git a/src/sharers/linked-in.js b/src/sharers/linked-in.js new file mode 100644 index 0000000..b321fc2 --- /dev/null +++ b/src/sharers/linked-in.js @@ -0,0 +1,37 @@ +let shownWarning = false; + +export function render(_text, _rawText, refUrl) { + if (!shownWarning) { + shownWarning = true; + console.warn("LinkedIn doesn't allow sharing links with custom titles anymore, so the main point of ShareThis " + + "(sharing a portion of text) couldn't be accomplished. You're encouraged to share your URLs with other, " + + "more conventional means, like the official LinkedIn share plugin. See " + + "https://docs.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/plugins/share-plugin"); + } + const url = this.getShareUrl(refUrl); + + return `` + + "" + + "" + + ""; +} + +export function getShareUrl(refUrl) { + return `https://www.linkedin.com/sharing/share-offsite/?url=${encodeURIComponent(refUrl)}`; +} + +export function action(event, item) { + event.preventDefault(); + const popup = item.ownerDocument.defaultView.open( + item.firstChild.href, + "share_via_linked_in", + "height=440,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,width=640" + ); + popup.opener = null; +} + +export const name = "linked-in"; diff --git a/test/sharers/linked-in.js b/test/sharers/linked-in.js new file mode 100644 index 0000000..d0a1060 --- /dev/null +++ b/test/sharers/linked-in.js @@ -0,0 +1,100 @@ +/* eslint-disable consistent-return, no-undef, no-unused-expressions */ +import { parse } from "url"; + +import chai, { expect } from "chai"; +import { stub, match } from "sinon"; +import sinonChai from "sinon-chai"; +import { JSDOM } from "jsdom"; + +import * as linkedInSharer from "../../src/sharers/linked-in"; + +chai.use(sinonChai); + +describe("LinkedIn sharer", () => { + it("must have name 'linked-in'", () => { + expect(linkedInSharer.name).to.equal("linked-in"); + }); + + it("must render a link to LinkedIn", () => { + const html = linkedInSharer.render("", "", "path/to/whatever"); + const { window } = new JSDOM(html); + const anchor = window.document.querySelector("a[href^='https://www.linkedin.com/']"); + expect(anchor).to.not.be.null; + }); + + describe("`getShareUrl` method", () => { + it("must have a `getShareUrl` helper method", () => { + expect(typeof linkedInSharer.getShareUrl).to.equal("function"); + }); + + it("must have a `url` parameter in the sharing URL", () => { + const shareUrl = linkedInSharer.getShareUrl("path/to/whatever"); + const parsed = parse(shareUrl, true); + expect(parsed.query).to.eql({ url: "path/to/whatever" }); + }); + }); + + describe("`action` method", () => { + it("must have a `action` method", () => { + expect(typeof linkedInSharer.action).to.equal("function"); + }); + + it("must prevent the event's default", () => { + const html = linkedInSharer.render("", "", "path/to/whatever"); + const { window } = new JSDOM(html); + const event = new window.Event("click"); + const preventStub = stub(event, "preventDefault"); + stub(window, "open").returns({}); + + linkedInSharer.action(event, window.document.body); + expect(preventStub.called).to.be.true; + }); + + it("must open a new window", () => { + const html = linkedInSharer.render("", "", "path/to/whatever"); + const { window } = new JSDOM(html); + const event = new window.Event("click"); + const openStub = stub(window, "open"); + openStub.returns({}); + + linkedInSharer.action(event, window.document.body); + expect(openStub.calledOnce).to.be.true; + }); + + it("must open a new window named \"share_via_linked_in\"", () => { + const html = linkedInSharer.render("", "", "path/to/whatever"); + const { window } = new JSDOM(html); + const event = new window.Event("click"); + const openStub = stub(window, "open"); + openStub.returns({}); + + linkedInSharer.action(event, window.document.body); + expect(openStub).to.have.been.calledWith(match.any, "share_via_linked_in", match.any); + }); + + it("must open a new window with the link provided by `getShareUrl`", () => { + const html = linkedInSharer.render("", "", "path/to/whatever"); + const { window } = new JSDOM(html); + const event = new window.Event("click"); + const openStub = stub(window, "open"); + openStub.returns({}); + + const url = linkedInSharer.getShareUrl("path/to/whatever"); + + linkedInSharer.action(event, window.document.body); + expect(openStub).to.have.been.calledWith(url, match.any, match.any); + }); + + it("must nullify the popup's `opener` property", () => { + const html = linkedInSharer.render("", "", "path/to/whatever"); + const { window } = new JSDOM(html); + const event = new window.Event("click"); + const openStub = stub(window, "open"); + const popup = {}; + openStub.returns(popup); + + linkedInSharer.action(event, window.document.body); + expect(popup.opener).to.be.null; + }); + }); +});