Skip to content

Instantly share code, notes, and snippets.

@iam-raihan
Created July 1, 2020 23:24
Show Gist options
  • Save iam-raihan/0300f3a68be4403b250f8deca3ab8d92 to your computer and use it in GitHub Desktop.
Save iam-raihan/0300f3a68be4403b250f8deca3ab8d92 to your computer and use it in GitHub Desktop.
triggers notification when shops are available in a campaign in evaly.com.bd
var campaign = 'evaly-earthquake-july-2020-99c4e2'; // set campaign slug
var pageUrl = `https://evaly.com.bd/campaign/${campaign}`;
var apiUrl = `https://api.evaly.com.bd/core/campaigns/public/${campaign}/shops`;
var delay = 5000; // ms
var showNotification = (msg, shopUrl = null) => {
var notification = new Notification('Evaly', {
icon: 'https://evaly.com.bd/static/images/icons/android-chrome-144x144.png',
body: msg,
});
notification.onclick = function() {
window.open(shopUrl ?? pageUrl);
};
};
var onShopsFound = (shops) => {
var slug = shops[0].slug;
var shopUrl = `https://evaly.com.bd/campaign/shop/${slug}/${campaign}`;
showNotification(`Shops Found = ${shops.length}`, shopUrl);
};
var processData = (data) => {
if (data.success) {
var shops = data.data;
if (shops.length === 0) {
console.log('No Shop Found');
} else {
onShopsFound(shops);
}
} else {
showNotification(`Failed to fetch shops`);
}
};
var callApi = () => {
fetch(apiUrl)
.then(res => {
if (res.status === 200) {
res.json().then(data => processData(data));
} else {
showNotification(`Error! Status Code: ${res.status}`);
}
})
.catch(err => showNotification(err));
};
var worker = setInterval(callApi, delay);
var stop = () => clearInterval(worker);
@iam-raihan
Copy link
Author

Instruction:

  1. visit a campaign page like "https://evaly.com.bd/campaign/{campaign_id}"
  2. copy campaign slug (id) from url
  3. change "campaign" in the script (line: 1)
  4. paste the script in browser console and press enter.

That's it.
Then it will start working with 5 second interval and notify you when any shop is available.
Click on the notification to open available shop in new tab.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment