Restock tweaks - use a single regex, tidy up height detection (#2125)

pull/2129/head
dgtlmoon 11 months ago committed by GitHub
parent 96793890f8
commit 677082723c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,3 +1,10 @@
// 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)
function isItemInStock() { function isItemInStock() {
// @todo Pass these in so the same list can be used in non-JS fetchers // @todo Pass these in so the same list can be used in non-JS fetchers
const outOfStockTexts = [ const outOfStockTexts = [
@ -56,6 +63,7 @@ function isItemInStock() {
'품절' '품절'
]; ];
const vh = Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0);
function getElementBaseText(element) { function getElementBaseText(element) {
// .textContent can include text from children which may give the wrong results // .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 // scan only immediate TEXT_NODEs, which will be a child of the element
@ -66,19 +74,13 @@ function isItemInStock() {
return text.toLowerCase().trim(); return text.toLowerCase().trim();
} }
const negateOutOfStockRegexs = [ const negateOutOfStockRegex = new RegExp('([0-9] in stock|add to cart)', 'ig');
'[0-9] in stock'
]
var negateOutOfStockRegexs_r = [];
for (let i = 0; i < negateOutOfStockRegexs.length; i++) {
negateOutOfStockRegexs_r.push(new RegExp(negateOutOfStockRegexs[0], 'g'));
}
// The out-of-stock or in-stock-text is generally always above-the-fold // 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 // 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 // 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 // and it should be atleast 100px from the top to ignore items in the toolbar, sometimes menu items like "Coming soon" exist
const elementsToScan = Array.from(document.getElementsByTagName('*')).filter(element => element.getBoundingClientRect().top + window.scrollY <= window.innerHeight && element.getBoundingClientRect().top + window.scrollY >= 100); const elementsToScan = Array.from(document.getElementsByTagName('*')).filter(element => element.getBoundingClientRect().top + window.scrollY <= vh && element.getBoundingClientRect().top + window.scrollY >= 100);
var elementText = ""; var elementText = "";
@ -94,13 +96,11 @@ function isItemInStock() {
if (elementText.length) { if (elementText.length) {
// try which ones could mean its in stock // try which ones could mean its in stock
for (let i = 0; i < negateOutOfStockRegexs.length; i++) { if (negateOutOfStockRegex.test(elementText)) {
if (negateOutOfStockRegexs_r[i].test(elementText)) {
return 'Possibly in stock'; return 'Possibly in stock';
} }
} }
} }
}
// OTHER STUFF THAT COULD BE THAT IT'S OUT OF STOCK // OTHER STUFF THAT COULD BE THAT IT'S OUT OF STOCK
for (let i = elementsToScan.length - 1; i >= 0; i--) { for (let i = elementsToScan.length - 1; i >= 0; i--) {

Loading…
Cancel
Save