Last active
December 13, 2020 23:57
-
-
Save kt0/7a37a5556a2a7f100ea68c6ce2ffb6b5 to your computer and use it in GitHub Desktop.
Fix only app offers in digikala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Replace digikala mobile links | |
// @namespace KiTO | |
// @description Replace digikala mobile links | |
// @author KiTO | |
// @version 0.0.1 | |
// @grant none | |
// @run-at document-end | |
// @match https://www.digikala.com/* | |
// @match http://www.digikala.com/* | |
// ==/UserScript== | |
(function () { | |
setInterval(fixMobileLink, 500); | |
})(); | |
function replace(li) { | |
const $link = li.querySelector('.js-product-url') | |
const $box = li.querySelector('.c-product-box') | |
const $image = li.querySelector('.js-url') | |
const $title = li.querySelector('.c-product-box__title') | |
const id = $link.dataset.id | |
const title = $box.title | |
const product = findProduct(id) | |
const newUrl = `/product/dkp-${id}` | |
$link.href = newUrl | |
if (product && product.image_src) { | |
$image.innerHTML = `<img alt="" src="${product.image_src}" class="swiper-lazy swiper-lazy-loaded">` | |
} else { | |
$image.innerHTML = `<span class="c-product-box__title-special">Cannot find the image url</span>` | |
} | |
if ($title) $title.innerHTML = title | |
const $cart = li.querySelector('.c-product-box__add-to-cart-section'); | |
if ($cart) { | |
li.querySelector('.c-product-box__add-to-cart-section').style.display = 'block' | |
} | |
$link.dataset.checked = "checked" | |
} | |
function fixMobileLink() { | |
Array.from(document.getElementsByClassName('js-product-url')).forEach((item) => { | |
if (!item.href.includes('landings/new-app/') && item.dataset.checked !== "checked") return; | |
replace(item.parentElement); | |
}); | |
} | |
function finder (item) {return item.id === id} | |
function findProduct (id) { | |
id = +id | |
const W = window.wrappedJSObject | |
return ( | |
W.click_impression?.find(finder) || | |
W.carouselData?.map(c => c.products).flat().find(finder) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment