Last active
December 28, 2021 16:18
-
-
Save limi58/71dbb02803df0d0a1943cb2ae122abbb to your computer and use it in GitHub Desktop.
auto amazon of tampermonkey script
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 auto-amazon | |
// @namespace http://tampermonkey.net/auto-amazon | |
// @version 0.1 | |
// @description auto-amazon | |
// @author DebugMi | |
// @match https://www.amazon.cn/* | |
// @grant GM_log | |
// @grant GM_setValue | |
// @grant GM_getValue | |
// @grant GM_deleteValue | |
// @grant GM_listValues | |
// @run-at document-end | |
// ==/UserScript== | |
;(function () { | |
'use strict' | |
// search word | |
const searchWord = '机械键盘' | |
// find a word then click it | |
const findKeyWord = '斐尔克' | |
// result list scroll count | |
const listScrollCount = 5 | |
// detail page scroll count | |
const detailScrollCount = 10 | |
// to link after finish work | |
const homePage = 'https://www.amazon.cn/' | |
main() | |
function main() { | |
const task = getTask() || 'search' | |
log(`start, task is ${task}`) | |
switch (task) { | |
case 'search': | |
handleSearch() | |
break | |
case 'findItem': | |
handleFindItem() | |
break | |
case 'detailPage': | |
handleDetailPage() | |
break | |
case 'end': | |
handleEnd() | |
break | |
default: | |
break | |
} | |
} | |
function handleSearch() { | |
sleep(3000).then(() => { | |
log('start search') | |
const $input = $('#twotabsearchtextbox') | |
const $submit = $('form.nav-searchbar') | |
changeTask('findItem') | |
$input.value = searchWord | |
$submit.submit() | |
}) | |
} | |
function handleFindItem() { | |
randomScroll(listScrollCount, 0, () => { | |
const $item = $('.s-result-item[data-component-type]', true)[0] | |
const itemUrl = $item.querySelector('.s-product-image-container a').href | |
changeTask('detailPage') | |
location.assign(itemUrl) | |
}) | |
} | |
function handleDetailPage() { | |
randomScroll(detailScrollCount, 0, () => { | |
window.scrollTo(0, random(0, 100)) | |
sleep(random(0, 2000)).then(() => { | |
changeTask('end') | |
$('#add-to-cart-button').click() | |
}) | |
}) | |
} | |
function handleEnd() { | |
sleep(random(1000, 3000)).then(() => { | |
changeTask('search') | |
location.assign(homePage) | |
}) | |
} | |
function scroll(num) { | |
window.scrollBy(0, num) | |
} | |
function randomScroll(totalCount, curCount, cb) { | |
if (curCount > totalCount) { | |
return cb() | |
} | |
log(`scrolling...${totalCount}...${curCount}`) | |
return sleep(random(0, 3000)).then(() => { | |
scroll(random(0, 500)) | |
return randomScroll(totalCount, curCount + 1, cb) | |
}) | |
} | |
function random(min, max) { | |
min = Math.ceil(min) | |
max = Math.floor(max) | |
return Math.floor(Math.random() * (max - min) + min) | |
} | |
function $(selector, isAll) { | |
if (isAll) { | |
return document.querySelectorAll(selector) | |
} | |
return document.querySelector(selector) | |
} | |
function log(str) { | |
GM_log(str) | |
} | |
function getCache(key) { | |
return GM_getValue(key) | |
} | |
function setCache(key, value) { | |
return GM_setValue(key, value) | |
} | |
function removeCache(key) { | |
return GM_deleteValue(key) | |
} | |
function changeTask(nextTask) { | |
setCache('mi-task', nextTask) | |
} | |
function getTask() { | |
return getCache('mi-task') | |
} | |
function sleep(time) { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
resolve('') | |
}, time) | |
}) | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment