OpenGraphPageData.sys.mjs (1474B)
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 Open Graph (https://opengraphprotocol.org/) related data from a page. 7 */ 8 export const OpenGraphPageData = { 9 /** 10 * Collects the opengraph data from the page. 11 * 12 * @param {Document} document 13 * The document to collect from 14 * 15 * @returns {PageData} 16 */ 17 collect(document) { 18 let pageData = {}; 19 20 // Sites can technically define an Open Graph prefix other than `og:`. 21 // However, `og:` is one of the default RDFa prefixes and it's likely 22 // uncommon that sites use a custom prefix. If we find that metadata is 23 // missing for common sites due to this issue, we could consider adding a 24 // basic RDFa parser. 25 let openGraphTags = document.querySelectorAll("meta[property^='og:'"); 26 27 for (let tag of openGraphTags) { 28 // Strip "og:" from the property name. 29 let propertyName = tag.getAttribute("property").substring(3); 30 31 switch (propertyName) { 32 case "description": 33 pageData.description = tag.getAttribute("content"); 34 break; 35 case "site_name": 36 pageData.siteName = tag.getAttribute("content"); 37 break; 38 case "image": 39 pageData.image = tag.getAttribute("content"); 40 break; 41 } 42 } 43 44 return pageData; 45 }, 46 };