Bug 1740539: Get siteName, image and description from twitter cards. r=Standard8

Differential Revision: https://phabricator.services.mozilla.com/D130884
This commit is contained in:
Dave Townsend 2021-11-16 13:13:26 +00:00
Родитель fe86d983fc
Коммит 3dda387a13
5 изменённых файлов: 83 добавлений и 1 удалений

Просмотреть файл

@ -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();

Просмотреть файл

@ -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;
},
};

Просмотреть файл

@ -18,6 +18,7 @@ EXTRA_JS_MODULES.pagedata += [
"PageDataSchema.jsm",
"PageDataService.jsm",
"SchemaOrgPageData.jsm",
"TwitterPageData.jsm",
]
FINAL_TARGET_FILES.actors += [

Просмотреть файл

@ -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(
`
<html>
<head>
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@nytimes">
<meta name="twitter:creator" content="@SarahMaslinNir">
<meta name="twitter:title" content="Parade of Fans for Houstons Funeral">
<meta name="twitter:description" content="NEWARK - The guest list and parade of limousines">
<meta name="twitter:image" content="http://graphics8.nytimes.com/images/2012/02/19/us/19whitney-span/19whitney-span-articleLarge.jpg">
</head>
<body>
</body>
</html>
`,
{
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: {},
}
);
});

Просмотреть файл

@ -9,3 +9,4 @@ head = head.js
[test_pagedata_schema.js]
[test_opengraph.js]
[test_schemaorg.js]
[test_twitter.js]