This repository has been archived by the owner on Dec 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split extraction methods and ensure they return null when no match is…
… found. Open Graph and selector-based extraction are now separate forms of extraction instead of a single, "fallback" extraction method.
- Loading branch information
Michael Kelly
committed
Oct 10, 2018
1 parent
3af22ed
commit 150cd9b
Showing
4 changed files
with
75 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/. */ | ||
|
||
/** | ||
* Product extraction via Open Graph tags. | ||
*/ | ||
|
||
const OPEN_GRAPH_PROPERTY_VALUES = { | ||
title: 'og:title', | ||
image: 'og:image', | ||
price: 'og:price:amount', | ||
}; | ||
|
||
/** | ||
* Returns any product information available on the page from Open Graph <meta> | ||
* tags. | ||
*/ | ||
export default function extractProduct() { | ||
const extractedProduct = {}; | ||
for (const [feature, propertyValue] of Object.entries(OPEN_GRAPH_PROPERTY_VALUES)) { | ||
const metaEle = document.querySelector(`meta[property='${propertyValue}']`); | ||
|
||
// Fail early if any required tags aren't found. | ||
if (!metaEle) { | ||
return null; | ||
} | ||
|
||
extractedProduct[feature] = metaEle.getAttribute('content'); | ||
} | ||
|
||
return extractedProduct; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.