Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restored LinkedIn sharer with limited functionality (fixes #37)
- Loading branch information
1 parent
41f4b51
commit 0cf6a62
Showing
6 changed files
with
158 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 `<a href="${url}" target="_blank" rel="noopener nofollow noreferrer">` | ||
+ "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 16 16\">" | ||
+ "<path d=\"M13.632 13.635h-2.37V9.922c0-.886-.018-2.025-1.234-2.025-1.235 0-1.424.964" | ||
+ "-1.424 1.96v3.778h-2.37V6H8.51v1.04h.03c.318-.6 1.092-1.233 2.247-1.233 2.4 0 " | ||
+ "2.845 1.58 2.845 3.637v4.188zM3.558 4.955c-.762 0-1.376-.617-1.376-1.377 0-.758" | ||
+ ".614-1.375 1.376-1.375.76 0 1.376.617 1.376 1.375 0 .76-.617 1.377-1.376 1.377z" | ||
+ "m1.188 8.68H2.37V6h2.376v7.635z\" fill=\"currentcolor\"/>" | ||
+ "</svg></a>"; | ||
} | ||
|
||
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}); | ||
}); | ||
}); |