Skip to content

Instantly share code, notes, and snippets.

@Trungfc
Last active May 4, 2022 01:08
Show Gist options
  • Save Trungfc/3ee243561e6ff52699ec639fcbd2fced to your computer and use it in GitHub Desktop.
Save Trungfc/3ee243561e6ff52699ec639fcbd2fced to your computer and use it in GitHub Desktop.
(function(window){
// You can enable the strict mode commenting the following line
// 'use strict';
// This function will contain all our code
function myLibrary(){
var thiz = {};
// Just create a property to our library object.
/**
* Checkout price is up on the item price.
*/
thiz.MIN_PROFIT = 7.00;
thiz.MIN_PROFIT_PERCENTAGE = 0.25; // 25%
thiz.MAX_PROFIT = 30.00;
thiz.MAX_PROFIT_PERCENTAGE = 0.5; // 50%
thiz.TAX = 0.1; //10%
thiz.MIN_PRICE_2X2 = 10.00;
thiz.MAX_PRICE_2X2 = 30.00;
thiz.MAX_PRICE_TO_CHECKOUT_AMZ = 1000;
thiz.CHECKOUT_PRICE_AMZ = [
//[100, 16.5],
[40, 19]
//,
//[20, 19]
];
thiz.MAX_PRICE_TO_CHECKOUT_EBAY = 1000;
thiz.CHECKOUT_PRICE_EBAY = [
[0, 20]];
thiz.MAX_PRICE_TO_CHECKOUT_WALMART = 1000;
thiz.CHECKOUT_PRICE_WALMART = [
[0, 16.5]];
thiz.USDVND = 22.9;
// Regex
thiz.NUMERIC_REGEXP = /[-]{0,1}[\d]*[.]{0,1}[\d]+/g;
// LINK SEARCH
thiz.ET_SEARCH_PRF = "https://www.etsy.com/search?";
thiz.AZ_SEARCH_PRF = "https://www.amazon.com/s?k=";
// Title for element
thiz.AMZ_SERACH_10 = "Az.10 từ";
thiz.AMZ_SEARCH_10_MAX_PRICE = "Az.Giá ≤ ";
thiz.ET_SEARCH_10 = "Et.10 từ";
thiz.ET_SEARCH_10_MAX_PRICE = "Et.Giá ≤ ";
thiz.SAVE = "Save";
thiz.SAVE_ALL = "Save All";
thiz.SAVED = "Saved";
thiz.COPY_SAVED = "Copy Saved Things";
thiz.CLEAR_SAVED = "Clear All";
thiz.SEPARATOR = "_;__";
thiz.TOTAL_SAVED = "total-saved";
thiz.SAVED_KEYS = "saved-keys";
thiz.ET_SEARCH_10_MAX_PRICE = "Et.Giá ≤ ";
thiz.ET_TITLE_MAX_LENGTH = 140;
// Attribute for element
thiz.ATTR_DATA_KEY = "data-key";
thiz.ATTR_DATA_VALUE = "data-value";
// Class for element
thiz.CLASS_FF_A_TAG = "ff-a-tag-ext";
thiz.CLASS_COPY_SINGLE_LINES = "ff-copy-single-lines-ext";
thiz.CLASS_AMZ_COPY_SINGLE_LINES = "ff-amz-copy-single-lines-ext";
thiz.CLASS_COPY_DESCRIPTION = "ff-copy-description-ext";
thiz.CLASS_COPY_VARIATION_INFO = "ff-copy-variation-info-ext";
thiz.CLASS_STATISTIC_BESS_VARIATIONS = "ff-start-statsitic-best-variation-ext";
thiz.CLASS_SHOWN_VARIATION = 'ff-shown-variations-ext';
thiz.CLASS_SHOW_BESS_VARIATIONS = 'ff-show-best-seller-variations-ext';
thiz.LABEL_STATISTIC_BESS_VARIATIONS = "Thống kê Variation bán chạy nhất";
thiz.CLASS_COPY_BEST_VARIATIONS = 'ff-copy-best-variations-ext';
thiz.LABEL_COPY_BEST_VARIATIONS = "Copy Best Variations";
thiz.CLASS_MIN_PRICE = "ff-min-price-ext";
thiz.CLASS_ADVICE_PRICE = "ff-advice-price-ext";
thiz.CLASS_UNLIMITED_PRICE = "ff-unlimited-price-ext";
thiz.CLASS_SELECTED = "ff-selected-ext";
// Order
thiz.CLASS_COPY_ORDER = "ff-copy-order-ext";
thiz.CLASS_SAVE_ORDER = "ff-save-order-ext";
thiz.CLASS_COPY_SAVED = "ff-copy-saved-ext";
thiz.CLASS_CLEAR_SAVED = "ff-clear-saved-ext";
thiz.CLASS_SAVE_ALL = "ff-save-all-ext";
// Default
thiz.DEFAULT_CLASS = "ff-default-ext";
thiz.DEFAULT_STYLE = "width:80px;background-color:#008000;color:white;top:20px;left:20px;padding:2px 3px;font-size:16px;z-index:5;cursor:pointer;";
thiz.ORANGE_STYLE = "width:80px;background-color:darkorange;color:white;top:20px;left:20px;padding:2px 3px;font-size:16px;z-index:5;cursor:pointer;";
thiz.AFFTER_ORANGE_STYLE = "width:80px;background-color:darkorange;color:white;top:20px;left:2px 20px;padding:3px;margin-left:5px;font-size:16px;z-index:5;cursor:pointer;";
thiz.AFFTER_GREEN_STYLE = "background-color:green;color:white;top:20px;left:20px;padding:2px 3px;margin-left:5px;font-size:16px;z-index:5;cursor:pointer;";
thiz.AFFTER_RED_STYLE = "width:80px;background-color:red;color:white;top:20px;left:20px;padding:2px 3px;margin-left:5px;font-size:16px;z-index:5;cursor:pointer;";
thiz.STYLE_STICKY = "position: -webkit-sticky;position: sticky;";
// Other for element
thiz.DELAY_TIME = 1000;
thiz.getMinPriceToSell = function (from, price, listPrice) {
var checkoutRate = 1;
switch(from){
case 'amazon':
checkoutRate = getCheckoutRate(price, thiz.MAX_PRICE_TO_CHECKOUT_AMZ, thiz.CHECKOUT_PRICE_AMZ);
break;
case 'walmart':
checkoutRate = getCheckoutRate(price, thiz.MAX_PRICE_TO_CHECKOUT_WALMART, thiz.CHECKOUT_PRICE_WALMART);
break;
case 'ebay':
checkoutRate = getCheckoutRate(price, thiz.MAX_PRICE_TO_CHECKOUT_EBAY, thiz.CHECKOUT_PRICE_EBAY);
break;
}
const checkoutPrice = checkoutRate * listPrice * (1+thiz.TAX);
var minProfit = thiz.MIN_PROFIT > checkoutPrice * thiz.MIN_PROFIT_PERCENTAGE ? thiz.MIN_PROFIT : checkoutPrice * thiz.MIN_PROFIT_PERCENTAGE;
var minPrice = (parseFloat(checkoutPrice) + parseFloat(minProfit)) /0.92 + 0.45;
return minPrice.toFixed(2);
}
thiz.addMinPriceElement = function (element, minPrice) {
var embed_min_price = '<span class="'+thiz.CLASS_MIN_PRICE+'" style="width:80px;background-color:#008000;color:white;top:20px;left:20px;padding-right:3px;padding-left:3px;padding-top:0;font-size:16px;z-index:5;cursor:pointer;">'+minPrice+'</span>';
element.append(embed_min_price);
}
thiz.addElement = function (element, data, clazz) {
var elClass = clazz? clazz: thiz.DEFAULT_CLASS
var embed_min_price = '<span class="'+elClass+'" style="width:80px;background-color:#008000;color:white;top:20px;left:20px;padding-right:3px;padding-left:3px;padding-top:0;font-size:16px;z-index:5;cursor:pointer;">'+data+'</span>';
element.append(embed_min_price);
}
thiz.addOrUpdateElement = function (el_root, title, clazz, insertPosition, style, data) {
var elClass = clazz? clazz: thiz.DEFAULT_CLASS;
var elStyle = style? style: thiz.DEFAULT_STYLE;
// check if element with class existing
var el = findElement(el_root,insertPosition, clazz);
if (el && el.length) {
el.html(title);
if (style) {
el.attr('style', style);
}
if (data) {
el.attr('data', data);
}
} else {
var el_inserted = '<span class="'+elClass+'" style="'+elStyle+'" data="'+data+'">'+title+'</span>';
insertElement(el_root, insertPosition, el_inserted);
}
}
thiz.addSearchLinks = function (element,name,listPrice, minPrice, insertPosition) {
// const thiz.ET_SEARCH_PRF = "https://www.etsy.com/search?";
// Create search etsy link
const QUERY_HOLDER = "_QUERY_HOLDER_";
const LABEL_HOLDER = "_LABEL_HOLDER_";
const CLASS_HOLDER = "_CLASS_HOLDER_";
//var query = name.replace(' ','+');
var a_tag_et_search ='<a class="'+CLASS_HOLDER+'" rel="noreferrer" target="_blank" href="'+ QUERY_HOLDER +'" style="width:80px;background-color:#FF9933;color:white;top:20px;left:20px;padding-right:3px;padding-left:3px;padding-top:0;margin-left:5px;font-size:16px;z-index:5;cursor:pointer;">'+LABEL_HOLDER+'</a>';
var embed_search_etsy = "";
var queryFirts10Words = createKeywords(name);
// Link search Et first 10 words in title
embed_search_etsy += a_tag_et_search.replace(CLASS_HOLDER,thiz.CLASS_FF_A_TAG + " " + thiz.CLASS_UNLIMITED_PRICE).replace(LABEL_HOLDER, thiz.ET_SEARCH_10).replace(QUERY_HOLDER, thiz.ET_SEARCH_PRF+"q="+queryFirts10Words);
expectPrice = parseInt(getAdvicePrice(listPrice)) + 1;
var max_parameter = 'max=' + expectPrice;
// Link search with advice Price
embed_search_etsy += a_tag_et_search.replace(CLASS_HOLDER,thiz.CLASS_FF_A_TAG + " " + thiz.CLASS_ADVICE_PRICE).replace(LABEL_HOLDER, thiz.ET_SEARCH_10_MAX_PRICE+expectPrice).replace(QUERY_HOLDER, thiz.ET_SEARCH_PRF+max_parameter+'&q='+queryFirts10Words).replace("background-color:#FF9933","background-color:#bf2424");
var etSearchMaxPrice = minPrice * 1.15; // 15% higher thann min price to sell
etSearchMaxPrice = parseInt(etSearchMaxPrice) + 1;
max_parameter = 'max=' + etSearchMaxPrice;
// Link search with min price
embed_search_etsy += a_tag_et_search.replace(CLASS_HOLDER,thiz.CLASS_FF_A_TAG + " " + thiz.CLASS_MIN_PRICE).replace(LABEL_HOLDER, thiz.ET_SEARCH_10_MAX_PRICE+etSearchMaxPrice).replace(QUERY_HOLDER, thiz.ET_SEARCH_PRF+max_parameter+'&q='+queryFirts10Words);
// Link search amazon
//const thiz.AZ_SEARCH_PRF = "https://www.amazon.com/s?k=";
embed_search_etsy += a_tag_et_search.replace(CLASS_HOLDER,thiz.CLASS_FF_A_TAG + " " + thiz.CLASS_UNLIMITED_PRICE).replace(LABEL_HOLDER, "Az Seach").replace(QUERY_HOLDER, thiz.AZ_SEARCH_PRF+name.replaceAll(" ", "+")).replace("background-color:#FF9933","background-color:#131921");
insertElement(element, insertPosition, embed_search_etsy);
}
function insertElement(el_root, insertPosition, el_inserted) {
switch(insertPosition){
case 'after':
el_root.after(el_inserted);
break;
case 'before':
el_root.before(el_inserted);
break;
case 'prepend':
el_root.prepend(el_inserted);
break;
default:
el_root.append(el_inserted);
}
}
function findElement(el_root, insertPosition, clazz) {
var el = false;
switch(insertPosition){
case 'after':
el = el_root.parent().find('.'+clazz);
break;
case 'before':
el = el_root.parent().find('.'+clazz);
break;
case 'prepend':
el = el_root.find('.'+clazz);
break;
case 'append':
el = el_root.find('.'+clazz);
break;
}
return el;
}
function createKeywords(name) {
const MAX_KEYWORD = 10;
// Remove the the common words from name to create keywords
var keywords = name.replaceAll('-',' ').split(' ').filter(function(item,i,allItems){
return (!/and/i.test(item) && !/the/i.test(item));
});
if (keywords.length > MAX_KEYWORD) {
return keywords.filter(function(item,i,allItems){
return i < MAX_KEYWORD;
}).join('+');
} else{
return keywords.join('+');
}
}
thiz.updateSearchLinks = function (name,listPrice, minPrice) {
// const thiz.ET_SEARCH_PRF = "https://www.etsy.com/search?";
var queryFirts10Words = createKeywords(name);
// Link search unlimited price
// var el_searchUnlimitedPrice = $('.'+thiz.CLASS_FF_A_TAG + "." + thiz.CLASS_UNLIMITED_PRICE);
// if (el_searchUnlimitedPrice.length) {
// el_searchUnlimitedPrice.hide();
// el_searchUnlimitedPrice.attr('href',thiz.ET_SEARCH_PRF+"q="+queryFirts10Words);
// setTimeout(function () {
// el_searchUnlimitedPrice.show();
// }, thiz.DELAY_TIME);
// }
// Link search ADVICE price
var expectPrice = parseInt(getAdvicePrice(listPrice)) + 1;
var max_parameter = 'max=' + expectPrice;
var el_searchAdvicePrice = $('.'+thiz.CLASS_FF_A_TAG + "." + thiz.CLASS_ADVICE_PRICE);
if (el_searchAdvicePrice.length) {
el_searchAdvicePrice.hide();
el_searchAdvicePrice.attr('href',thiz.ET_SEARCH_PRF+max_parameter+'&q='+queryFirts10Words);
el_searchAdvicePrice.html(thiz.ET_SEARCH_10_MAX_PRICE+expectPrice);
setTimeout(function () {
el_searchAdvicePrice.show();
}, thiz.DELAY_TIME);
}
// Link search MIN price
var el_searchMinPrice = $('.'+thiz.CLASS_FF_A_TAG + "." + thiz.CLASS_MIN_PRICE);
if (el_searchMinPrice.length) {
var etSearchMaxPrice = minPrice * 1.15; // 15% higher thann min price to sell
etSearchMaxPrice = parseInt(etSearchMaxPrice) + 1;
max_parameter = 'max=' + etSearchMaxPrice;
el_searchMinPrice.hide();
el_searchMinPrice.attr('href',thiz.ET_SEARCH_PRF+max_parameter+'&q='+queryFirts10Words);
el_searchMinPrice.html(thiz.ET_SEARCH_10_MAX_PRICE+etSearchMaxPrice);
setTimeout(function () {
el_searchMinPrice.show();
}, thiz.DELAY_TIME);
}
}
thiz.copy = function (element, data) {
var backgroundColor;
var $this = $(element);
var title = $this.text();
if (data != ''){
var $temp = $("<textarea>");
$("body").append($temp);
$temp.val(data).select();
document.execCommand("copy");
$temp.remove();
$this.html('Copied');
} else{
backgroundColor = $this.css('background-color');
$this.css('background-color', '#FF0000');
$this.html('Lỗi');
}
setTimeout(function () {
if (backgroundColor) {
$this.css('background-color', backgroundColor);
}
$this.html(title);
}, 2000);
}
thiz.showStatisticReview = function (setTypes, reviewReport, element, insertPosition) {
var statistic = [];
var data = '<br>';
for (let type of setTypes) {
statistic = [];
for(var key in reviewReport){
if (key.startsWith(type)) {
statistic.push({key:key, value:reviewReport[key], sort:reviewReport[key]});
}
}
statistic.sort(thiz.compareSort);
data += '<div style="margin-bottom:15px"><b class="'+thiz.CLASS_COPY_BEST_VARIATIONS+'" style="background-color:#bf2424;color:white;padding:3px 5px;cursor:pointer;">'+ type + ':</b> ';
for (var i = 0; i < statistic.length && i < 7; i++) {
var variation = statistic[i].key.replace(type,'').replace(':','').trim();
data += '<span class="'+thiz.CLASS_SHOWN_VARIATION+'"style="'+ thiz.DEFAULT_STYLE+'" variation-type="'+type+'" variation-value="'+variation+'"> ' + variation + ':</span> ' + statistic[i].value + ' ';
}
data += '</div>';
}
//ffDropshipLibs.addElement($('div.rate-detail', iframeReview.contents()),data);
thiz.addOrUpdateElement(element ,data, thiz.CLASS_SHOW_BESS_VARIATIONS,insertPosition,"background-color:white");
return statistic;
//$("html, body").animate({ scrollTop: 0 }, "slow");
}
thiz.compareSort = function ( a, b ) {
if ( a.sort < b.sort ){
return 1;
}
if ( a.sort > b.sort ){
return -1;
}
return 0;
}
// =====BEGIN: SAVE and CLEAR, COPY MULTIPLE ITEMS/ORDERS
thiz.modifySavedList = function (element,addOnly) {
var data = element.attr(thiz.ATTR_DATA_VALUE);
var key = element.attr(thiz.ATTR_DATA_KEY);
var savedKeys = localStorage.getItem(thiz.SAVED_KEYS);
var totalSaved = localStorage.getItem(thiz.TOTAL_SAVED);
if (localStorage.getItem(key)) {
if (!addOnly) {
// This mean this order have saved before and now we need to remove it
localStorage.removeItem(key);
savedKeys = savedKeys.replace(key,'').replace(thiz.SEPARATOR+thiz.SEPARATOR, thiz.SEPARATOR);
totalSaved = parseInt(totalSaved) - 1;
element.html(thiz.SAVE);
element.css('background-color','darkorange');
}
} else{
localStorage.setItem(key, data);
element.html(thiz.SAVED);
element.css('background-color','green');
totalSaved = totalSaved? parseInt(totalSaved) + 1: 1;
savedKeys = savedKeys? savedKeys + thiz.SEPARATOR + key: key;
}
localStorage.setItem(thiz.SAVED_KEYS, savedKeys);
localStorage.setItem(thiz.TOTAL_SAVED, totalSaved);
return totalSaved;
}
// Save ALL
thiz.saveAll = function (el_root) {
$("."+thiz.CLASS_SAVE_ORDER).each(function (ind, ele) {
thiz.modifySavedList($(ele), true);
});
var totalSaved = localStorage.getItem(thiz.TOTAL_SAVED);
thiz.addOrUpdateElement(el_root, thiz.COPY_SAVED+" ("+totalSaved+")", thiz.CLASS_COPY_SAVED, "before");
}
// Clear All
thiz.clearAllSaved = function (el_root) {
localStorage.clear();
thiz.addOrUpdateElement(el_root, thiz.COPY_SAVED+"("+0+")", thiz.CLASS_COPY_SAVED, "before");
$("."+thiz.CLASS_SAVE_ORDER).each(function (ind, ele) {
$(ele).html(thiz.SAVE);
$(ele).css('background-color','darkorange');
});
}
// Copy Saved on click
thiz.onclickCopySaved = function () {
$('body').on('click', '.'+thiz.CLASS_COPY_SAVED, function (e) {
var data = [];
var keys = localStorage.getItem(thiz.SAVED_KEYS).split(thiz.SEPARATOR),
i = keys.length;
while ( i-- ) {
data.push( localStorage.getItem(keys[i]) );
}
thiz.copy(this, data.join("\n"));
});
}
// Clear Saved on click
thiz.onclickClearSaved = function () {
$('body').on('click', '.'+thiz.CLASS_CLEAR_SAVED, function (event) {
//localStorage.clear();
var keys = localStorage.getItem(thiz.SAVED_KEYS).split(thiz.SEPARATOR),
i = keys.length;
while ( i-- ) {
localStorage.removeItem(keys[i]);
}
localStorage.removeItem(thiz.SAVED_KEYS);
var $this = $(this);
var title = $this.text();
$this.html('Cleared');
setTimeout(function() {
$this.html(title);
}, 1000);
});
}
// =====END: SAVE and CLEAR, COPY MULTIPLE ITEMS/ORDERS
// Aliexpress
thiz.getTitleFromAliexpress = function(el_brand, el_title, maxLength) {
// Title span#productTitle
var brand = el_brand.text().replace(' Store','').replace('Official','').trim();
//var el_title = el_productInfo.find('h1.product-title-text');
var title = el_title.text().replaceAll(brand,'').replaceAll(', ',' ').replaceAll(',',' ').replaceAll('/ ',' ').replaceAll('/',' ').replaceAll('| ',' ').replaceAll('|','').trim();
// Remove duplicate and non-keyword if title have more than 140 characters
if (title.length > maxLength) {
title = title.split(' ').filter(function(item,i,allItems){
return i==allItems.indexOf(item);
}).join(' ');
}
return title;
}
function getAdvicePrice(listPrice) {
var expectPrice1 = parseFloat(listPrice) + parseFloat(Math.max(thiz.MAX_PROFIT_PERCENTAGE * listPrice, thiz.MIN_PRICE_2X2));
var expectPrice2 = parseFloat(listPrice) + parseFloat(Math.max(thiz.MAX_PROFIT_PERCENTAGE * listPrice, thiz.MAX_PRICE_2X2));
return (listPrice < thiz.MIN_PRICE_2X2? expectPrice1 :
(listPrice > thiz.MAX_PRICE_2X2 ?
expectPrice2 :
listPrice * 2.2)
) /0.92 + 0.45;
}
function getCheckoutRate(price, MAX_PRICE_TO_CHECKOUT, CHECKOUT_PRICE){
if(price > MAX_PRICE_TO_CHECKOUT)
return 1;
for(var i = 0; i < CHECKOUT_PRICE.length; i ++){
if (price >= CHECKOUT_PRICE[i][0]) {
return CHECKOUT_PRICE[i][1]/thiz.USDVND;
}
}
return 1;
}
// =====BEGIN: common
thiz.gotoElement = function ($element, position){
var $window = $(window);
var elementTop = $element.offset().top;
var scrollIt = elementTop;
switch(position){
case "top":
scrollIt = elementTop;
break;
case "center":
var elementHeight = $element.height(),
viewportHeight = $window.height();
scrollIt = elementTop - ((viewportHeight - elementHeight) / 2);
break;
}
$window.scrollTop(scrollIt);
}
thiz.getNumeric = function (text) {
var numerics = text.match(thiz.NUMERIC_REGEXP);
if (numerics != null && numerics.length) {
return parseFloat(numerics[0]);
}
return NaN;
}
thiz.notifyMe = function (msg) {
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
}
// Let's check whether notification permissions have already been granted
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
var notification = new Notification(msg);
}
// Otherwise, we need to ask the user for permission
else if (Notification.permission !== "denied") {
Notification.requestPermission().then(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
var notification = new Notification(msg);
}
});
}
// At last, if the user has denied notifications, and you
// want to be respectful there is no need to bother them any more.
}
// =====END: common
return thiz;
}
// We need that our library is globally accesible, then we save in the window
if(typeof(window.ffDropshipLibs) === 'undefined'){
window.ffDropshipLibs = myLibrary();
}
})(window); // We send the window variable within our function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment