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

Fix: Foreign Affairs update front-end. #3407

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
119 changes: 83 additions & 36 deletions Foreign Affairs.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
{
"translatorID": "4ab6d49c-d94e-4a9c-ae9a-3310c44ba612",
"label": "Foreign Affairs",
"creator": "Sebastian Karcher, Philipp Zumstein",
"creator": "Sebastian Karcher, Philipp Zumstein, Wenzhi Dave Ding",
"target": "^https?://www\\.foreignaffairs\\.com",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2023-08-23 12:37:06"
"lastUpdated": "2025-01-10 07:28:35"
}

/*
***** BEGIN LICENSE BLOCK *****

Copyright © 2016-2022 Sebastian Karcher & Philipp Zumstein
Copyright © 2016-2025 Sebastian Karcher & Philipp Zumstein & Wenzhi Dave Ding

This file is part of Zotero.

Expand All @@ -37,7 +37,7 @@


function detectWeb(doc, _url) {
if (doc.getElementsByClassName('article-body-text').length) {
if (doc.getElementsByClassName('article').length) {
return "magazineArticle";
}
else if (getSearchResults(doc, true)) {
Expand All @@ -47,19 +47,23 @@ function detectWeb(doc, _url) {
}

function getSearchResults(doc, checkOnly) {
let isIssues = /^https:\/\/[^/]+\/issues\/.+/.test(doc.location.href);
var items = {};
var found = false;
var rows = doc.querySelectorAll('article.article-card > a, div.article-data > h2.title > a');

let isIssues = /^\/issues\/.+/.test(doc.location.pathname);
let isSearch = /^\/search\/.+/.test(doc.location.pathname);

let rows = [];
if (isIssues) {
rows = doc.querySelectorAll('h3 > a');
}
else if (isSearch) {
rows = doc.querySelectorAll('h2 > a');
}

for (let row of rows) {
let href = row.href;
let title;
if (isIssues) {
title = text(row, ".article-card-title");
}
else {
title = ZU.trimInternal(row.textContent);
}
let title = ZU.trimInternal(row.textContent);
if (!href || !title) continue;
if (checkOnly) return true;
found = true;
Expand All @@ -83,22 +87,20 @@ async function doWeb(doc, url) {

async function scrape(doc, url = doc.location.href) {
var item = new Zotero.Item("magazineArticle");
var author = text(doc, '.article-byline-author');
var tags = doc.querySelectorAll('.article-footer--tag-item');
let issueNode = doc.querySelector(".article-header--metadata-date > a");

let issueNode = doc.querySelector(".topper__issue");
if (issueNode) {
var volumeTitle = ZU.trimInternal(issueNode.textContent.trim());
// the digits are yyyy/vol/num
let issueMatch = issueNode.href.match(/\/issues\/\d+\/(\d+)\/(\d+)$/);
if (volumeTitle) {
item.setExtra('Volume Title', volumeTitle);
}

let issueMatch = issueNode.parentNode.href.match(/\/issues\/\d+\/(\d+)\/(\d+)$/);
if (issueMatch) {
item.volume = issueMatch[1];
item.issue = issueMatch[2];
}
}
if (volumeTitle) {
if (!item.extra) item.extra = "";
item.extra += `\nVolume Title: ${volumeTitle}`;
}

item.date = attr(doc, 'meta[property="article:published_time"]', 'content');
item.title = attr(doc, 'meta[property="og:title"]', 'content');
Expand All @@ -107,23 +109,29 @@ async function scrape(doc, url = doc.location.href) {
item.date = ZU.strToISO(item.date);
}

var author = doc.querySelector('.topper__byline').textContent.trim();
author = author.replace("Reviewed by ", "");
let authors = author.split(/, and|and |, /);
for (let aut of authors) {
item.creators.push(ZU.cleanAuthor(aut, "author"));
}

var tags = doc.querySelectorAll(
'a[href^="/regions/"].text-decoration-none, '
+ 'a[href^="/topics/"].text-decoration-none, '
+ 'a[href^="/tags/"].text-decoration-none'
);
for (let tag of tags) {
item.tags.push(tag.textContent);
}
item.url = url;
item.url = url.split('?')[0];
item.attachments.push({ document: doc, title: "Snapshot" });
item.publicationTitle = "Foreign Affairs";
item.ISSN = "0015-7120";
item.language = "en-US";
item.complete();
}


/** BEGIN TEST CASES **/
var testCases = [
{
Expand All @@ -132,6 +140,12 @@ var testCases = [
"defer": true,
"items": "multiple"
},
{
"type": "web",
"url": "https://www.foreignaffairs.com/search/arkansas",
"defer": true,
"items": "multiple"
},
{
"type": "web",
"url": "https://www.foreignaffairs.com/reviews/capsule-review/2003-05-01/history-argentina-twentieth-century",
Expand All @@ -146,16 +160,16 @@ var testCases = [
"creatorType": "author"
}
],
"extra": "Volume Title: May/June 2003",
"volume": "82",
"issue": "3",
"date": "2003-05-01",
"ISSN": "0015-7120",
"abstractNote": "A fascinating and well-translated account of Argentina's misadventures over the last century by one of that country's brightest historians. Absorbing vast amounts of British capital and tens of thousands of European immigrants, Argentina began the century with great promise. In 1914, with half of its population still foreign, a dynamic society had emerged that was both open and mobile.",
"extra": "Volume Title: May/June 2003",
"issue": "3",
"language": "en-US",
"libraryCatalog": "Foreign Affairs",
"publicationTitle": "Foreign Affairs",
"url": "https://www.foreignaffairs.com/reviews/capsule-review/2003-05-01/history-argentina-twentieth-century",
"volume": "82",
"attachments": [
{
"title": "Snapshot",
Expand All @@ -164,10 +178,13 @@ var testCases = [
],
"tags": [
{
"tag": "Argentina"
"tag": " Americas"
},
{
"tag": " Argentina"
},
{
"tag": "Western Hemisphere"
"tag": " South America"
}
],
"notes": [],
Expand Down Expand Up @@ -209,6 +226,9 @@ var testCases = [
{
"tag": "Arms Control & Disarmament"
},
{
"tag": "Intelligence"
},
{
"tag": "Defense & Military"
},
Expand All @@ -228,11 +248,14 @@ var testCases = [
"tag": "Nuclear Weapons & Proliferation"
},
{
"tag": "Obama Administration"
"tag": "Barack Obama Administration"
},
{
"tag": "Persian Gulf"
},
{
"tag": "Sanctions"
},
{
"tag": "Security"
},
Expand Down Expand Up @@ -302,6 +325,21 @@ var testCases = [
{
"tag": "North America"
},
{
"tag": "World"
},
{
"tag": "Globalization"
},
{
"tag": "Finance"
},
{
"tag": "Politics & Society"
},
{
"tag": "Inequality"
},
{
"tag": "United States"
}
Expand Down Expand Up @@ -347,6 +385,21 @@ var testCases = [
},
{
"tag": "South Asia"
},
{
"tag": "Politics & Society"
},
{
"tag": "Demography"
},
{
"tag": "Gender"
},
{
"tag": "Narendra Modi"
},
{
"tag": "Business"
}
],
"notes": [],
Expand Down Expand Up @@ -427,12 +480,6 @@ var testCases = [
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://www.foreignaffairs.com/search/argentina",
"defer": true,
"items": "multiple"
}
]
/** END TEST CASES **/
Loading