{
	"translatorID": "8df4f61b-0881-4c85-9186-05f457edb4d3",
	"label": "PhilPapers",
	"creator": "Sebastian Karcher",
	"target": "^https?://phil(papers|archive)\\.org/",
	"minVersion": "3.0",
	"maxVersion": "",
	"priority": 100,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "gcsibv",
	"lastUpdated": "2025-08-19 15:22:16"
}

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

	Copyright © 2023 Sebastian Karcher

	This file is part of Zotero.

	Zotero is free software: you can redistribute it and/or modify
	it under the terms of the GNU Affero General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Zotero is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU Affero General Public License for more details.

	You should have received a copy of the GNU Affero General Public License
	along with Zotero. If not, see <http://www.gnu.org/licenses/>.

	***** END LICENSE BLOCK *****
*/


function detectWeb(doc, url) {
	let isLoggedIn = doc.querySelector('a[href*="/logout"]');
	// Some pages include embedded BibTeX
	// For ones that don't, we need to be authenticated or export will fail
	if (url.includes('/rec/') && (isLoggedIn || text(doc, '#bibtex'))) {
		return 'journalArticle';
	}
	else if (isLoggedIn && getSearchResults(doc, true)) {
		return 'multiple';
	}
	return false;
}

function getSearchResults(doc, checkOnly) {
	var items = {};
	var found = false;
	var rows = doc.querySelectorAll('.entryList .citation>a');
	for (let row of rows) {
		let href = row.href;
		let title = ZU.trimInternal(row.textContent);
		if (!href || !title) continue;
		if (checkOnly) return true;
		found = true;
		items[href] = title;
	}
	return found ? items : false;
}

async function doWeb(doc, url) {
	if (detectWeb(doc, url) == 'multiple') {
		let items = await Zotero.selectItems(getSearchResults(doc, false));
		if (!items) return;
		for (let url of Object.keys(items)) {
			await scrape(await requestDocument(url));
		}
	}
	else {
		await scrape(doc, url);
	}
}

function idFromUrl(url) {
	return url.match(/\/rec\/([A-Z-\d]+)/)[1];
}

async function scrape(doc, url) {
	let id = idFromUrl(url);

	let bibText = text(doc, "#bibtex");
	if (!bibText) {
		bibText = await requestText(`/item.pl?id=${id}&format=bib`);
	}
	let translator = Zotero.loadTranslator("import");
	translator.setTranslator('9cb70025-a888-4a29-a210-93ec52da40d4');
	translator.setString(bibText);
	translator.setHandler('itemDone', (_obj, item) => {
		let isPhilArchive = new URL(url).hostname === 'philarchive.org';
		if (isPhilArchive || doc.querySelector('.download-options a[href*="/archive/"]')) {
			item.url = url.replace(/[#?].*$/, '');
			item.attachments.push({
				title: 'Full Text PDF',
				mimeType: 'application/pdf',
				url: `/archive/${id}`,
			});
		}
		else {
			item.attachments.push({
				url,
				title: 'Snapshot',
				mimeType: 'text/html'
			});
		}
		item.libraryCatalog = isPhilArchive ? 'PhilArchive' : 'PhilPapers';
		item.complete();
	});
	await translator.translate();
}

/** BEGIN TEST CASES **/
var testCases = [
	{
		"type": "web",
		"url": "https://philpapers.org/rec/COROCA-4",
		"items": [
			{
				"itemType": "journalArticle",
				"title": "Observation, Character, and a Purely First-Person Point of View",
				"creators": [
					{
						"firstName": "Josep E.",
						"lastName": "Corbí",
						"creatorType": "author"
					}
				],
				"date": "2011",
				"DOI": "10.1007/s12136-011-0124-2",
				"issue": "4",
				"itemID": "Corbi2011-COROCA-4",
				"libraryCatalog": "PhilPapers",
				"pages": "311–328",
				"publicationTitle": "Acta Analytica",
				"volume": "26",
				"attachments": [
					{
						"title": "Snapshot",
						"mimeType": "text/html"
					}
				],
				"tags": [],
				"notes": [],
				"seeAlso": []
			}
		]
	},
	{
		"type": "web",
		"url": "https://philarchive.org/rec/RAYNGF",
		"items": [
			{
				"itemType": "journalArticle",
				"title": "Norm-Based Governance for a New Era: Lessons From Climate Change and Covid-19",
				"creators": [
					{
						"firstName": "Leigh",
						"lastName": "Raymond",
						"creatorType": "author"
					},
					{
						"firstName": "Daniel",
						"lastName": "Kelly",
						"creatorType": "author"
					},
					{
						"firstName": "Erin",
						"lastName": "Hennes",
						"creatorType": "author"
					}
				],
				"date": "2021",
				"itemID": "Raymond2021-RAYNGF",
				"libraryCatalog": "PhilArchive",
				"pages": "1–14",
				"publicationTitle": "Perspectives on Politics",
				"shortTitle": "Norm-Based Governance for a New Era",
				"url": "https://philarchive.org/rec/RAYNGF",
				"volume": "1",
				"attachments": [
					{
						"title": "Full Text PDF",
						"mimeType": "application/pdf"
					}
				],
				"tags": [],
				"notes": [],
				"seeAlso": []
			}
		]
	},
	{
		"type": "web",
		"url": "https://philarchive.org/rec/LANTEO-39",
		"items": [
			{
				"itemType": "journalArticle",
				"title": "The Ethics of Partiality",
				"creators": [
					{
						"firstName": "Benjamin",
						"lastName": "Lange",
						"creatorType": "author"
					}
				],
				"date": "2022",
				"DOI": "10.1111/phc3.12860",
				"issue": "8",
				"itemID": "Lange2022-LANTEO-39",
				"libraryCatalog": "PhilArchive",
				"pages": "1–15",
				"publicationTitle": "Philosophy Compass",
				"url": "https://philarchive.org/rec/LANTEO-39",
				"volume": "1",
				"attachments": [
					{
						"title": "Full Text PDF",
						"mimeType": "application/pdf"
					}
				],
				"tags": [],
				"notes": [],
				"seeAlso": []
			}
		]
	}
]
/** END TEST CASES **/
