-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
226 lines (219 loc) · 5.81 KB
/
content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
let isShoppingPage;
let productName = "";
window.onload = reevaluatePage();
function reevaluatePage() {
chrome.storage.local.get("optIn", (response) => {
if (!response.optIn) {
chrome.runtime.sendMessage({ msgName: "displayOptin" });
} else {
pageEval();
}
});
}
function pageEval() {
const dom = document.getElementsByTagName("html")[0].innerHTML;
const shopWords = [
/add-to-basket/i,
/addtocartBtn/i,
/gl-product-card-container/i,
/instock/i,
/in-stock/i,
/js-detail-product/i,
/product-block/i,
/product-block-js/i,
/product-card/i,
/product-container/i,
/product-content/i,
/product-declinaison/i,
/product-detail/i,
/product-detail__buy/i,
/product-detail__desc/i,
/product-detail__details/i,
/product-detail__images/i,
/product-gallery/i,
/product-gallery*/i,
/product-grid__content/i,
/product-grid__container/i,
/product-grid__image/i,
/product-grid__intro/i,
/product-grid__item/i,
/product-grid__link/i,
/product-grid*/i,
/ProductGrid__Wrapper*/i,
/product-grid-wider-exp/i,
/product-image/i,
/product-image*/i,
/product-image-container/i,
/product--image-container/i,
/product-image-photo/i,
/product-image-wrapper/i,
/product-image__link/i,
/product-images/i,
/product-images*/i,
/product-images-container/i,
/product-info/i,
/product-info-block/i,
/productitem/i,
/productitem_imagewrapper/i,
/product-item-info/i,
/product-list/i,
/product-list*/i,
/product-listing*/i,
/product-listing__content/i,
/product-listing-item__wrapper/i,
/product-listing__wrapper/i,
/productlistwrapper/i,
/product-name/i,
/product-options/i,
/product-options*/i,
/product-options-wrapper/i,
/product-page/i,
/product-price/i,
/product-primary/i,
/product-primary*/i,
/product-primary-image/i,
/product-slider/i,
/product-slider-container/i,
/productthumbnail/i,
/productthumbnails/i,
/product-textbadges/i,
/product-tile/i,
/product-tile-badges/i,
/product-view/i,
/product-wrapper/i,
/pvproduct/i,
/pvproduct*/i,
/return-policy/i,
/return_policy/i,
/shopcart/i,
/shopcontent/i,
/shop-module/i,
/shopify-section/i,
/shopify-section*/i,
/"shopping"/i,
/"shopping*"/i,
/shopping-bag-empty/i,
/shopping-window/i,
/soldout-image/i,
/woocommerce-products-header__title page-title/i,
/add to bag/i,
/add to basket/i,
/add to cart/i,
/bought this/i,
/browse products/i,
/browse products*/i,
/buy it now/i,
/buy this now/i,
/buy now/i,
/checkout/i,
/coupons & deals/i,
/coupons & deals/i,
/featured products/i,
/find a store/i,
/free shipping/i,
/get free shipping/i,
/get free shipping*/i,
/in stock/i,
/online order/i,
/online ordering/i,
/online orders/i,
/order cancellation/i,
/order cancellation policy/i,
/order now/i,
/order pickup/i,
/order policy/i,
/order policies/i,
/order status/i,
/our stores/i,
/pre-order/i,
/preorder/i,
/product catalog/i,
/refund policy/i,
/return policy/i,
/returns & exchanges/i,
/returns & exchanges/i,
/returns & refunds/i,
/returns & refunds/i,
/returns & repairs/i,
/returns & repairs/i,
/same day delivery/i,
/secure checkout/i,
/shipping &*/i,
/shipping &*/i,
/shipping & returns/i,
/shipping & returns/i,
/shipping info/i,
/shipping information/i,
/shipping policy/i,
/shop all/i,
/shop bestsellers/i,
/shop by category/i,
/shop by categories/i,
/shop by collection/i,
/shop by collections/i,
/shop by department/i,
/shop category/i,
/shop categories/i,
/shop collection/i,
/shop collections/i,
/shop now/i,
/shop now*/i,
/shop our bestsellers/i,
/shop popular/i,
/shop popular categories/i,
/shop product/i,
/shop product*/i,
/shop product range/i,
/shop the collection/i,
/shopping options/i,
/ssl secure checkout/i,
/track order/i,
/track your order/i,
/upcoming sales/i
];
const presentShopWords = [];
for (let i = 0; i < shopWords.length; i++) {
const inArray = shopWords[i].test(dom);
if (inArray) { // if words are present, add values.
presentShopWords.push(shopWords[i].toString());
}
}
if (presentShopWords.length > 0) { // if there's at least one shopWord present
chrome.runtime.sendMessage({ msgName: "PageEvaluated", shoppingPage: true, shopWords: presentShopWords });
identifyProduct();
isShoppingPage = true;
} else {
chrome.runtime.sendMessage({ msgName: "PageEvaluated", shoppingPage: false });
isShoppingPage = false;
}
}
function identifyProduct() {
productName = "";
// gets all html elements that are images and have an ancestor with a classname that includes the word product
const productElements = document.querySelectorAll("[class*='product'] * img");
if (productElements[0]) {
// .alt is the alt text for the image
productName = productName + productElements[0].alt + " ";
}
productName = productName + document.title;
if (productName !== "") {
chrome.runtime.sendMessage({ msgName: "ProductIdentified", productName: productName });
}
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
switch (request.msgName) {
case "isShoppingPage?":
sendResponse({ isShoppingPage: isShoppingPage });
break;
case "reevaluatePage":
reevaluatePage();
sendResponse({ response: "reevaluated" });
break;
case "productIdentified?":
sendResponse({ productName: productName });
break;
default:
console.error(request, sender);
}
return true;
});