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

Squarespace #3243

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Changes from 9 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
226 changes: 226 additions & 0 deletions Squarespace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
{
"translatorID": "a0dfd108-1d71-4bb1-b787-af64fd0c41cc",
"label": "Squarespace",
"creator": "Thaddeus Hetling",
"target": "",
"minVersion": "5.0",
"maxVersion": "",
"priority": 300,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2024-01-29 21:47:54"
}

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

Copyright © 2024 Thaddeus Hetling

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) {
if (doc.querySelector('.sqs-block-content') && doc.querySelector('meta[property="og:type"][content="article"]')) {
return 'blogPost';
}
else if (getSearchResults(doc, true)) {
return 'multiple';
}
return false;
}

function getSearchResults(doc, checkOnly) {
var items = {};
var found = false;
var rows = doc.querySelectorAll('.sqs-search-container-item');
for (let row of rows) {
let href = null;
try {
href = new URL(row.parentElement.attributes['data-url'].value, document.baseURI).href;
EruditeLying marked this conversation as resolved.
Show resolved Hide resolved
}
catch {
continue;
}

let title = text(row, '.sqs-title');

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 getMetadataItem(doc, prop) {
return attr(doc, 'html>head>meta[itemprop="' + prop + '"]', 'content');
Copy link
Member

Choose a reason for hiding this comment

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

Any reason we need the extra specification and not just meta[itemprop="${prop}"]? <meta> tags are valid in the body and we don't normally restrict the selector unless there's a very good performance argument for doing so (e.g. detection in Embedded Metadata).

Copy link
Author

Choose a reason for hiding this comment

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

I copied the selector from some other translator (Embedded Metadata probably). I'll simplify it

}

async function scrape(doc, url = doc.location.href) {
let translator = Zotero.loadTranslator('web');
// Embedded Metadata
translator.setTranslator('951c027d-74ac-47d4-a107-9c3069ab7b48');
translator.setDocument(doc);

translator.setHandler('itemDone', (_obj, item) => {
item.websiteType = 'Squarespace blog';
EruditeLying marked this conversation as resolved.
Show resolved Hide resolved
item.date = getMetadataItem(doc, 'datePublished');
let author = getMetadataItem(doc, 'author');
if (author) {
item.creators.push({
lastName: author,
fieldMode: 1,
creatorType: 'author'
});
}
if (item.abstractNote) item.abstractNote = ZU.unescapeHTML(item.abstractNote);
item.tags = Array.prototype.slice.call(doc.querySelectorAll('.blog-item-tag-wrapper a')).map((tag) => ZU.trim(tag.textContent));
EruditeLying marked this conversation as resolved.
Show resolved Hide resolved
item.complete();
});

let em = await translator.getTranslatorObject();
em.itemType = 'blogPost';
await em.doWeb(doc, url);
}
/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "https://www.minimist.ca/articles/my-favourite-squarespace-css-hacks",
"items": [
{
"itemType": "blogPost",
"title": "My Favourite Squarespace CSS Hacks",
"creators": [
{
"lastName": "Karl Patton",
EruditeLying marked this conversation as resolved.
Show resolved Hide resolved
"fieldMode": 1,
"creatorType": "author"
}
],
"date": "2018-06-14T12:00:00-0400",
"abstractNote": "One of the main concerns I've heard from clients and others thinking about using Squarespace is that they don't want their website to look and act like everyone else's site and look like a template.",
"blogTitle": "Minimist Website Design | Squarespace Expert & Website Designer",
"language": "en-CA",
"url": "https://www.minimist.ca/articles/my-favourite-squarespace-css-hacks",
"websiteType": "Squarespace blog",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://www.thestyledsquare.com/blog-content/how-to-reduce-spacer-block-height-with-css-in-squarespace",
"items": [
{
"itemType": "blogPost",
"title": "How to reduce spacer block height with CSS in Squarespace (7.0 & 7.1)",
"creators": [
{
"lastName": "The Styled Square",
"fieldMode": 1,
"creatorType": "author"
}
],
"date": "2021-12-10T11:12:50-0800",
"abstractNote": "The Spacer Block element is an essential tool for your Squarespace website design, but by default, the Spacer Block has a set height that can be too large for some instances. In this tutorial, we’re sharing copy & paste CSS to adjust the Spacer Block minimum height for more flexibility whi",
"blogTitle": "The Styled Square | Premium Squarespace Website Templates",
"language": "en-US",
"url": "https://www.thestyledsquare.com/blog-content/how-to-reduce-spacer-block-height-with-css-in-squarespace",
"websiteType": "Squarespace blog",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://www.silvabokis.com/search?q=squarespace",
"items": "multiple"
},
{
"type": "web",
"url": "https://originalbox.co/blog/tags-squarespace-add-categories",
"items": [
{
"itemType": "blogPost",
"title": "What are tags on Squarespace and how to add categories",
"creators": [
{
"lastName": "Roshni Kahol",
"fieldMode": 1,
"creatorType": "author"
}
],
"date": "2020-10-31T17:28:38+0000",
"abstractNote": "Using the blog page as an example, we explain how to use tags on Squarespace. We also teach how to add, edit and delete categories on Squarespace.",
"blogTitle": "Original Box",
"language": "en-GB",
"url": "https://originalbox.co/blog/tags-squarespace-add-categories",
"websiteType": "Squarespace blog",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [
{
"tag": "2020"
},
{
"tag": "october 2020"
}
],
"notes": [],
"seeAlso": []
}
]
}
]
/** END TEST CASES **/