You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
5.0 KiB
135 lines
5.0 KiB
9 months ago
|
// Restock Detector
|
||
|
// (c) Leigh Morresi dgtlmoon@gmail.com
|
||
|
//
|
||
|
// Assumes the product is in stock to begin with, unless the following appears above the fold ;
|
||
|
// - outOfStockTexts appears above the fold (out of stock)
|
||
|
// - negateOutOfStockRegex (really is in stock)
|
||
|
|
||
2 years ago
|
function isItemInStock() {
|
||
10 months ago
|
// @todo Pass these in so the same list can be used in non-JS fetchers
|
||
|
const outOfStockTexts = [
|
||
|
' أخبرني عندما يتوفر',
|
||
|
'0 in stock',
|
||
|
'agotado',
|
||
|
'article épuisé',
|
||
|
'artikel zurzeit vergriffen',
|
||
|
'as soon as stock is available',
|
||
|
'ausverkauft', // sold out
|
||
|
'available for back order',
|
||
|
'back-order or out of stock',
|
||
|
'backordered',
|
||
|
'benachrichtigt mich', // notify me
|
||
|
'brak na stanie',
|
||
|
'brak w magazynie',
|
||
|
'coming soon',
|
||
|
'currently have any tickets for this',
|
||
|
'currently unavailable',
|
||
|
'dostępne wkrótce',
|
||
|
'en rupture de stock',
|
||
|
'ist derzeit nicht auf lager',
|
||
|
'item is no longer available',
|
||
|
'let me know when it\'s available',
|
||
|
'message if back in stock',
|
||
|
'nachricht bei',
|
||
|
'nicht auf lager',
|
||
|
'nicht lieferbar',
|
||
|
'nicht zur verfügung',
|
||
|
'niet beschikbaar',
|
||
|
'niet leverbaar',
|
||
9 months ago
|
'niet op voorraad',
|
||
10 months ago
|
'no disponible temporalmente',
|
||
|
'no longer in stock',
|
||
|
'no tickets available',
|
||
|
'not available',
|
||
|
'not currently available',
|
||
10 months ago
|
'not in stock',
|
||
10 months ago
|
'notify me when available',
|
||
10 months ago
|
'notify when available',
|
||
10 months ago
|
'não estamos a aceitar encomendas',
|
||
|
'out of stock',
|
||
|
'out-of-stock',
|
||
9 months ago
|
'prodotto esaurito',
|
||
10 months ago
|
'produkt niedostępny',
|
||
|
'sold out',
|
||
|
'sold-out',
|
||
|
'temporarily out of stock',
|
||
|
'temporarily unavailable',
|
||
|
'tickets unavailable',
|
||
|
'tijdelijk uitverkocht',
|
||
|
'unavailable tickets',
|
||
|
'we do not currently have an estimate of when this product will be back in stock.',
|
||
|
'we don\'t know when or if this item will be back in stock.',
|
||
|
'zur zeit nicht an lager',
|
||
|
'品切れ',
|
||
|
'已售完',
|
||
|
'품절'
|
||
|
];
|
||
2 years ago
|
|
||
9 months ago
|
const vh = Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0);
|
||
10 months ago
|
function getElementBaseText(element) {
|
||
|
// .textContent can include text from children which may give the wrong results
|
||
|
// scan only immediate TEXT_NODEs, which will be a child of the element
|
||
|
var text = "";
|
||
|
for (var i = 0; i < element.childNodes.length; ++i)
|
||
|
if (element.childNodes[i].nodeType === Node.TEXT_NODE)
|
||
|
text += element.childNodes[i].textContent;
|
||
|
return text.toLowerCase().trim();
|
||
|
}
|
||
2 years ago
|
|
||
9 months ago
|
const negateOutOfStockRegex = new RegExp('([0-9] in stock|add to cart)', 'ig');
|
||
2 years ago
|
|
||
10 months ago
|
// The out-of-stock or in-stock-text is generally always above-the-fold
|
||
|
// and often below-the-fold is a list of related products that may or may not contain trigger text
|
||
|
// so it's good to filter to just the 'above the fold' elements
|
||
|
// and it should be atleast 100px from the top to ignore items in the toolbar, sometimes menu items like "Coming soon" exist
|
||
9 months ago
|
const elementsToScan = Array.from(document.getElementsByTagName('*')).filter(element => element.getBoundingClientRect().top + window.scrollY <= vh && element.getBoundingClientRect().top + window.scrollY >= 100);
|
||
2 years ago
|
|
||
10 months ago
|
var elementText = "";
|
||
2 years ago
|
|
||
10 months ago
|
// REGEXS THAT REALLY MEAN IT'S IN STOCK
|
||
|
for (let i = elementsToScan.length - 1; i >= 0; i--) {
|
||
|
const element = elementsToScan[i];
|
||
|
elementText = "";
|
||
|
if (element.tagName.toLowerCase() === "input") {
|
||
|
elementText = element.value.toLowerCase();
|
||
|
} else {
|
||
|
elementText = getElementBaseText(element);
|
||
|
}
|
||
2 years ago
|
|
||
10 months ago
|
if (elementText.length) {
|
||
|
// try which ones could mean its in stock
|
||
9 months ago
|
if (negateOutOfStockRegex.test(elementText)) {
|
||
|
return 'Possibly in stock';
|
||
10 months ago
|
}
|
||
2 years ago
|
}
|
||
|
}
|
||
2 years ago
|
|
||
10 months ago
|
// OTHER STUFF THAT COULD BE THAT IT'S OUT OF STOCK
|
||
|
for (let i = elementsToScan.length - 1; i >= 0; i--) {
|
||
|
const element = elementsToScan[i];
|
||
|
if (element.offsetWidth > 0 || element.offsetHeight > 0 || element.getClientRects().length > 0) {
|
||
|
elementText = "";
|
||
|
if (element.tagName.toLowerCase() === "input") {
|
||
|
elementText = element.value.toLowerCase();
|
||
|
} else {
|
||
|
elementText = getElementBaseText(element);
|
||
|
}
|
||
2 years ago
|
|
||
10 months ago
|
if (elementText.length) {
|
||
|
// and these mean its out of stock
|
||
|
for (const outOfStockText of outOfStockTexts) {
|
||
|
if (elementText.includes(outOfStockText)) {
|
||
|
return outOfStockText; // item is out of stock
|
||
|
}
|
||
|
}
|
||
|
}
|
||
2 years ago
|
}
|
||
|
}
|
||
|
|
||
10 months ago
|
return 'Possibly in stock'; // possibly in stock, cant decide otherwise.
|
||
2 years ago
|
}
|
||
|
|
||
|
// returns the element text that makes it think it's out of stock
|
||
10 months ago
|
return isItemInStock().trim()
|
||
|
|