Skip to content

Instantly share code, notes, and snippets.

@elincognito
Forked from sebastienvercammen/Code.gs
Created May 19, 2017 14:02
Show Gist options
  • Save elincognito/3c8b4a6aded929bf717d44eb4b808ae0 to your computer and use it in GitHub Desktop.
Save elincognito/3c8b4a6aded929bf717d44eb4b808ae0 to your computer and use it in GitHub Desktop.
Nintendo PTC Account Verifier for Gmail v2 with CORS proxy (via Google Scripts & jQuery)
/*
Automatically click all "Verify your email" links in the welcome e-mail from
Nintendo Pokémon Trainer Club's signup e-mails.
Only unread emails in inbox will be processed.
All processed e-mails will be marked as read, even if verification fails.
How to use:
1. Login to Gmail
2. Go to https://script.google.com/
3. Enter the code in Code.gs and index.html (if index.html doesn't exist, create it).
4. Click Publish, then run as web app.
5. You'll see the results on the page. Dev console can be used for debugging.
6. Enjoy
*/
// Serve HTML file.
function doGet() {
return HtmlService.createHtmlOutputFromFile('index.html');
}
function getAllEmailLinks(limit) {
// Prepare response.
var links = [];
// Start.
var threads = GmailApp.search('in:inbox is:unread subject:"Pokémon Trainer Club Activation"');
Logger.log("Found " + threads.length + " threads.");
threads.forEach(function(thread) {
var messages = thread.getMessages();
Logger.log("Found " + messages.length + " messages.");
messages.forEach(function(msg) {
// If we have a limit, follow it.
if (limit > 0 && links.length >= limit) {
return;
}
var value = msg.getBody()
.match(/verify your email/m);
if(msg.isInInbox() && value) {
var link = msg.getBody().match(/<a href="https:\/\/club.pokemon.com\/us\/pokemon-trainer-club\/activated\/([\w\d]+)"/);
if(link) {
var url = 'https://club.pokemon.com/us/pokemon-trainer-club/activated/' + link[1];
// Add to list for front-end.
links.push(url);
// We no longer use UrlFetchApp, so let's just mark as read.
msg.markRead();
}
}
});
});
Logger.log("Sent " + links.length + " URLs to front-end for verification.");
return links;
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(function() {
print('Started processing e-mails.');
// Settings.
var limit = 10; // Enable limit of emails to process. 0 to disable.
// Keep track.
var emails = [];
// Requests.
function getRequest(item) {
$.ajax({
url: item
}).done(onRequestSuccess).fail(function(jqXHR, textStatus, errorThrown) {
alert('Request failed: ' + textStatus);
});
}
// Callback.
function onRequestSuccess(data, textStatus, xhr) {
// Request successful.
var status = xhr.status;
if (status === 200) {
if (emails.length > 0) {
// Get next item.
item = emails.pop();
print('Processing next email. Items left in queue: ' + emails.length + '.');
// New request.
getRequest(item);
} else {
print('Finished processing emails.');
}
} else {
print('Unexpected response (' + status + '): ' + textStatus);
}
}
// Go.
function receivedEmailList(list) {
print('Received ' + list.length + ' emails for verification.');
// Update global for callback handler.
emails = list;
// Stop if we have no results.
if (emails.length === 0) {
alert('No unread PTC emails in inbox to verify.');
return;
}
// We have results. Set up AJAX for CORS proxy.
setupCorsProxy();
// Time for requests.
print('Starting first request.');
var item = emails.pop();
getRequest(item);
}
function setupCorsProxy() {
$.ajaxPrefilter( function (options) {
if (options.crossDomain && jQuery.support.cors) {
var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
options.url = http + '//cors-anywhere.herokuapp.com/' + options.url;
//options.url = "http://cors.corsproxy.io/url=" + options.url;
}
});
}
function print(txt) {
document.write(txt + '<br>');
}
google.script.run.withSuccessHandler(receivedEmailList).getAllEmailLinks(limit);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment