Created
December 21, 2018 01:51
-
-
Save danikaze/f550876792743ed2e9f7c42125c4df35 to your computer and use it in GitHub Desktop.
Automagically join the Rakuten lottery every day
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 Rakuten Lottery | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1.0 | |
// @description Automagically join the Rakuten lottery every day | |
// @author danikaze | |
// @homepage https://gist.github.com/danikaze/ | |
// @match *://*/* | |
// @grant GM_setValue | |
// @grant GM_getValue | |
// @grant GM_xmlhttpRequest | |
// @connect rakuten.co.jp | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const tm = new Date().getTime(); | |
/** | |
* Rakuten Lottery page referral could change, so we get the proper URL from the Top Page | |
*/ | |
function getLotteryUrl() { | |
return new Promise((resolve, reject) => { | |
function checkPage(res) { | |
const re = /<a href="(https:\/\/rd.rakuten.co.jp\/s\/\?R2=https%3A%2F%2Fkuji.rakuten.co.jp[^"]+)"/; | |
let match = re.exec(res.responseText); | |
if (match) { | |
resolve(decodeURIComponent(match[1])); | |
} else { | |
reject(); | |
} | |
} | |
const request = GM_xmlhttpRequest({ | |
method: 'GET', | |
url: 'https://www.rakuten.co.jp/', | |
onload: checkPage, | |
onerror: reject, | |
headers: { | |
Origin: 'https://www.rakuten.co.jp/', | |
} | |
}); | |
}); | |
} | |
/** | |
* Try to enter the Rakuten Lottery (need to be logged into rakuten.co.jp) | |
*/ | |
async function enterLottery() { | |
return new Promise((resolve, reject) => { | |
// Nacho: | |
// https://kuji.rakuten.co.jp/8d82d77188/dXlwUldJN3hmelVLbG9FY0s1bzRDdz09Cg==/accept?flash_version=undefined&os=MacIntel&useragent=Mozilla/5.0%20(Macintosh;%20Intel%20Mac%20OS%20X%2010_12_6)%20AppleWebKit/537.36%20(KHTML,%20like%20Gecko)%20Chrome/70.0.3538.110%20Safari/537.36 | |
// https://kuji.rakuten.co.jp/8d82d77188/dXlwUldJN3hmelVLbG9FY0s1bzRDdz09Cg==/decide?flash_version=undefined&os=MacIntel&useragent=Mozilla/5.0%20(Macintosh;%20Intel%20Mac%20OS%20X%2010_12_6)%20AppleWebKit/537.36%20(KHTML,%20like%20Gecko)%20Chrome/70.0.3538.110%20Safari/537.36 | |
function checkResult(res) { | |
debugger; | |
} | |
getLotteryUrl().then((url) => { | |
const request = GM_xmlhttpRequest({ | |
url, | |
method: 'GET', | |
onload: checkResult, | |
onerror: reject, | |
}); | |
}); | |
}); | |
} | |
/** | |
* Generate a ID for elements without conflicts | |
*/ | |
function getCssId(id) { | |
return `tm-rakuten-lottery-${tm}-${id}`; | |
} | |
/** | |
* Create a generic (styled) popup to display a title and a msg | |
*/ | |
function createPopup(title, msg) { | |
const popupElem = document.createElement('div'); | |
const headerId = getCssId('popup-header'); | |
const closeButtonId = getCssId('popup-button'); | |
const titleId = getCssId('popup-title'); | |
const msgId = getCssId('popup-msg'); | |
popupElem.id = getCssId('popup'); | |
popupElem.innerHTML = ` | |
<div id="${headerId}"> | |
<div id="${titleId}">${title}</div> | |
<div id="${closeButtonId}">✖︎</div> | |
</div> | |
<div id="${msgId}">${msg}</div> | |
`; | |
popupElem.style = ` | |
position: fixed; | |
top: 20px; | |
right: 20px; | |
z-index: 65000; | |
width: 200px; | |
border-radius: 8px; | |
box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.5); | |
`; | |
const headerElem = popupElem.querySelector(`#${headerId}`); | |
headerElem.style = ` | |
background: #b70700; | |
color: white; | |
padding: 5px; | |
padding-left: 10px; | |
border-top-right-radius: 8px; | |
border-top-left-radius: 8px; | |
`; | |
const titleElem = popupElem.querySelector(`#${titleId}`); | |
titleElem.style = ` | |
font-weight: bold; | |
`; | |
const closeButtonElem = popupElem.querySelector(`#${closeButtonId}`); | |
closeButtonElem.addEventListener('click', () => popupElem.parentElement.removeChild(popupElem)); | |
closeButtonElem.style = ` | |
position: absolute; | |
right: 7px; | |
top: 5px; | |
cursor: pointer; | |
`; | |
const msgElem = popupElem.querySelector(`#${msgId}`); | |
msgElem.style = ` | |
padding: 10px; | |
background: #fff; | |
border-bottom-right-radius: 8px; | |
border-bottom-left-radius: 8px; | |
`; | |
document.body.appendChild(popupElem); | |
} | |
/** | |
* When winning the lottery, show a message notifying about it | |
*/ | |
function showAtari(points) { | |
createPopup( | |
'TM Rakuten Lottery', | |
`You won <strong>${points === 1 ? '1 point' : `${points} points`}</strong> in today's Rakuten Lottery!`, | |
); | |
} | |
/** | |
* When not winning, still show a notification message about it | |
*/ | |
function showHazure() { | |
createPopup( | |
'TM Rakuten Lottery', | |
`You didn\'t win anything in today's Rakuten Lottery...`, | |
); | |
} | |
/** | |
* Check if it already entered for today | |
*/ | |
function needsToRun() { | |
var now = new Date(); | |
var m = now.getMonth(); | |
var d = now.getDate(); | |
var y = now.getFullYear(); | |
var today = new Date(y,m,d); | |
if (GM_getValue("lastRun", 0) < today) { | |
GM_setValue("lastRun", today); | |
return true; | |
} | |
return false; | |
} | |
/** | |
* EXECUTION | |
*/ | |
if (true || needsToRun()) { | |
enterLottery().then((result) => { | |
if (result.win) { | |
showAtari(result.points); | |
} else { | |
showHazure(); | |
} | |
}).catch(() => { | |
// if there's an error entering, set it to try again next time | |
GM_setValue("lastRun", 0); | |
}); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment