Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New translator for LinkedIn post pages. #3373

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions LinkedIn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{
"translatorID": "797eb77d-9596-450e-a589-1621dc34a057",
"label": "LinkedIn",
"creator": "Liz Lawley",
"target": "^https://www\\.linkedin\\.com/posts/",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2024-10-13 16:36:16"
}

// NOTE: This translator will only work if you are *not* logged into LinkedIn. Logged-in versions of the page do not include the necessary metadata. If you are logged into LinkedIn, you can open a private or incognito window to use the translator. //

function detectWeb(doc, url) {
if (url.includes('/posts/')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If scrape() only works when script[type="application/ld+json"] exists, we should check that here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

return "forumPost";
}
return false;
}

function doWeb(doc, url) {
scrape(doc, url);
}

function scrape(doc, url) {
var newItem = new Zotero.Item("forumPost");

// Extract and clean up the title (first 20 words of the description)
var rawDescription = ZU.xpathText(doc, "//meta[@property='og:description']/@content");
if (rawDescription) {
mamamusings marked this conversation as resolved.
Show resolved Hide resolved
var words = ZU.unescapeHTML(rawDescription).split(/\s+/);
var title = words.slice(0, 20).join(' ');
newItem.title = title + (words.length > 20 ? '...' : '');
}

newItem.url = url.replace(/[#?].*$/, '');

// Extract author name from page title
var pageTitle = ZU.xpathText(doc, "//title");
if (pageTitle) {
var titleParts = pageTitle.split(' on ');
if (titleParts.length > 1) {
var authorName = titleParts[0].trim();
newItem.creators.push(ZU.cleanAuthor(authorName, "author"));
}
}

// Extract date from JSON-LD
var jsonLD = doc.querySelector('script[type="application/ld+json"]');
if (jsonLD) {
try {
var data = JSON.parse(jsonLD.textContent);
if (data.datePublished) {
newItem.date = ZU.strToISO(data.datePublished);
}
}
catch (e) {
Zotero.debug("Failed to parse JSON-LD: " + e);
}
}

newItem.forumTitle = "LinkedIn";
newItem.postType = "LinkedIn Post";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if we really need this - it's a forumPost on forumTitle "LinkedIn".

newItem.websiteType = "Social Media";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto - I don't think this is necessary for a citation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure about this. LinkedIn posts are in kind of a gray area between forum posts and blog posts.


newItem.attachments.push({
title: "Snapshot",
document: doc
});
newItem.complete();
}

/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "https://www.linkedin.com/posts/emollick_we-hope-that-such-tools-may-help-us-to-gain-activity-7249843496746979328-7tXt/?utm_source=share&utm_medium=member_desktop",
"items": [
{
"itemType": "forumPost",
"title": "\"We hope that such tools may help us to gain novel insight into the psychology of an understudied pool of...",
"creators": [
{
"firstName": "Ethan",
"lastName": "Mollick",
"creatorType": "author"
}
],
"date": "2024-10-09",
"forumTitle": "LinkedIn",
"postType": "LinkedIn Post",
"url": "https://www.linkedin.com/posts/emollick_we-hope-that-such-tools-may-help-us-to-gain-activity-7249843496746979328-7tXt/?utm_source=share&utm_medium=member_desktop",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
}
]
/** END TEST CASES **/
Loading