TwitterPageData.sys.mjs (1168B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 /** 6 * Collects Twitter card (https://developer.twitter.com/en/docs/twitter-for-websites/) 7 * related data from a page. 8 */ 9 export const TwitterPageData = { 10 /** 11 * Collects the twitter data from the page. 12 * 13 * @param {Document} document 14 * The document to collect from 15 * 16 * @returns {PageData} 17 */ 18 collect(document) { 19 let pageData = {}; 20 21 let twitterTags = document.querySelectorAll("meta[name^='twitter:'"); 22 23 for (let tag of twitterTags) { 24 // Strip "twitter:" from the property name. 25 let propertyName = tag.getAttribute("name").substring(8); 26 27 switch (propertyName) { 28 case "site": 29 pageData.siteName = tag.getAttribute("content"); 30 break; 31 case "description": 32 pageData.description = tag.getAttribute("content"); 33 break; 34 case "image": 35 pageData.image = tag.getAttribute("content"); 36 break; 37 } 38 } 39 40 return pageData; 41 }, 42 };