From 3dda387a13192a4fcdf1d283b05633ac73e5b5b5 Mon Sep 17 00:00:00 2001 From: Dave Townsend Date: Tue, 16 Nov 2021 13:13:26 +0000 Subject: [PATCH] Bug 1740539: Get siteName, image and description from twitter cards. r=Standard8 Differential Revision: https://phabricator.services.mozilla.com/D130884 --- .../components/pagedata/PageDataSchema.jsm | 3 +- .../components/pagedata/TwitterPageData.jsm | 46 +++++++++++++++++++ browser/components/pagedata/moz.build | 1 + .../pagedata/tests/unit/test_twitter.js | 33 +++++++++++++ .../pagedata/tests/unit/xpcshell.ini | 1 + 5 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 browser/components/pagedata/TwitterPageData.jsm create mode 100644 browser/components/pagedata/tests/unit/test_twitter.js diff --git a/browser/components/pagedata/PageDataSchema.jsm b/browser/components/pagedata/PageDataSchema.jsm index bfab99002434..57fd1b4f69c5 100644 --- a/browser/components/pagedata/PageDataSchema.jsm +++ b/browser/components/pagedata/PageDataSchema.jsm @@ -18,6 +18,7 @@ XPCOMUtils.defineLazyModuleGetters(this, { OpenGraphPageData: "resource:///modules/pagedata/OpenGraphPageData.jsm", SchemaOrgPageData: "resource:///modules/pagedata/SchemaOrgPageData.jsm", Services: "resource://gre/modules/Services.jsm", + TwitterPageData: "resource:///modules/pagedata/TwitterPageData.jsm", }); XPCOMUtils.defineLazyGetter(this, "logConsole", function() { @@ -42,7 +43,7 @@ XPCOMUtils.defineLazyGetter(this, "logConsole", function() { * can and then we drop anything that is invalid once all data is joined. */ XPCOMUtils.defineLazyGetter(this, "DATA_COLLECTORS", function() { - return [SchemaOrgPageData, OpenGraphPageData]; + return [SchemaOrgPageData, OpenGraphPageData, TwitterPageData]; }); let SCHEMAS = new Map(); diff --git a/browser/components/pagedata/TwitterPageData.jsm b/browser/components/pagedata/TwitterPageData.jsm new file mode 100644 index 000000000000..5f926b919cd1 --- /dev/null +++ b/browser/components/pagedata/TwitterPageData.jsm @@ -0,0 +1,46 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +var EXPORTED_SYMBOLS = ["TwitterPageData"]; + +/** + * Collects Twitter card (https://developer.twitter.com/en/docs/twitter-for-websites/) + * related data from a page. + */ +const TwitterPageData = { + /** + * Collects the twitter data from the page. + * + * @param {Document} document + * The document to collect from + * + * @returns {PageData} + */ + collect(document) { + let pageData = {}; + + let twitterTags = document.querySelectorAll("meta[name^='twitter:'"); + + for (let tag of twitterTags) { + // Strip "twitter:" from the property name. + let propertyName = tag.getAttribute("name").substring(8); + + switch (propertyName) { + case "site": + pageData.siteName = tag.getAttribute("content"); + break; + case "description": + pageData.description = tag.getAttribute("content"); + break; + case "image": + pageData.image = tag.getAttribute("content"); + break; + } + } + + return pageData; + }, +}; diff --git a/browser/components/pagedata/moz.build b/browser/components/pagedata/moz.build index e0e913280135..1b9f7e3df02b 100644 --- a/browser/components/pagedata/moz.build +++ b/browser/components/pagedata/moz.build @@ -18,6 +18,7 @@ EXTRA_JS_MODULES.pagedata += [ "PageDataSchema.jsm", "PageDataService.jsm", "SchemaOrgPageData.jsm", + "TwitterPageData.jsm", ] FINAL_TARGET_FILES.actors += [ diff --git a/browser/components/pagedata/tests/unit/test_twitter.js b/browser/components/pagedata/tests/unit/test_twitter.js new file mode 100644 index 000000000000..1bc42de285b1 --- /dev/null +++ b/browser/components/pagedata/tests/unit/test_twitter.js @@ -0,0 +1,33 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/** + * Basic tests for twitter cards. + */ + +add_task(async function test_twitter_card() { + await verifyPageData( + ` + + + + + + + + + + + + + `, + { + siteName: "@nytimes", + description: "NEWARK - The guest list and parade of limousines", + image: + "http://graphics8.nytimes.com/images/2012/02/19/us/19whitney-span/19whitney-span-articleLarge.jpg", + data: {}, + } + ); +}); diff --git a/browser/components/pagedata/tests/unit/xpcshell.ini b/browser/components/pagedata/tests/unit/xpcshell.ini index f3ae14c2c1ab..7460035ee31d 100644 --- a/browser/components/pagedata/tests/unit/xpcshell.ini +++ b/browser/components/pagedata/tests/unit/xpcshell.ini @@ -9,3 +9,4 @@ head = head.js [test_pagedata_schema.js] [test_opengraph.js] [test_schemaorg.js] +[test_twitter.js]