Skip to content
This repository has been archived by the owner on Dec 3, 2020. It is now read-only.

Commit

Permalink
Split extraction methods and ensure they return null when no match is…
Browse files Browse the repository at this point in the history
… 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
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 24 deletions.
36 changes: 30 additions & 6 deletions src/extraction/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,42 @@

import config from 'commerce/config/content';
import extractProductWithFathom from 'commerce/extraction/fathom';
import extractProductWithFallback from 'commerce/extraction/fallback';
import extractProductWithFallback from 'commerce/extraction/selector';
import extractProductWithOpenGraph from 'commerce/extraction/open_graph';

/**
* Extraction methods are given the document object for the page, and must
* return either a valid ExtractedProduct, or null if a valid product could not
* be found.
*/
const EXTRACTION_METHODS = [
extractProductWithFathom,
extractProductWithFallback,
extractProductWithOpenGraph,
];

/**
* Perform product extraction, trying each method from EXTRACTION_METHODS in
* order until one of them returns a truthy result.
* @return {ExtractedProduct|null}
*/
function extractProduct() {
for (const extract of EXTRACTION_METHODS) {
const extractedProduct = extract(window.document);
if (extractedProduct) {
return extractedProduct;
}
}

return null;
}

/**
* Checks to see if any product information for the page was found,
* and if so, sends it to the background script.
*/
async function attemptExtraction() {
const extractedProduct = (
extractProductWithFathom(window.document)
|| extractProductWithFallback()
);

const extractedProduct = extractProduct();
if (extractedProduct) {
await browser.runtime.sendMessage({
from: 'content',
Expand Down
33 changes: 33 additions & 0 deletions src/extraction/open_graph.js
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@
* Features: title, image, price
*/

import extractionData from 'commerce/extraction/fallback/selectors';


const OPEN_GRAPH_PROPERTY_VALUES = {
title: 'og:title',
image: 'og:image',
price: 'og:price:amount',
};
import extractionData from 'commerce/extraction/selector/selectors';

/**
* Returns any extraction data found for the vendor based on the URL
Expand Down Expand Up @@ -54,22 +47,23 @@ function findValue(extractors) {

/**
* Returns any product information available on the page from CSS
* selectors if they exist, otherwise from Open Graph <meta> tags.
* selectors if they exist.
*/
export default function extractProduct() {
const extractedProduct = {};
const featureInfo = getFeatureInfo();
if (featureInfo) {
const extractedProduct = {};
for (const [feature, extractors] of Object.entries(featureInfo)) {
extractedProduct[feature] = findValue(extractors);
}
} else {
for (const [feature, propertyValue] of Object.entries(OPEN_GRAPH_PROPERTY_VALUES)) {
const metaEle = document.querySelector(`meta[property='${propertyValue}']`);
if (metaEle) {
extractedProduct[feature] = metaEle.getAttribute('content');
const featureValue = findValue(extractors);
if (!featureValue) {
return null;
}

extractedProduct[feature] = featureValue;
}

return extractedProduct;
}
return extractedProduct;

return null;
}
File renamed without changes.

0 comments on commit 150cd9b

Please sign in to comment.