Skip to content

Instantly share code, notes, and snippets.

@luisvonmuller
Created November 1, 2020 17:12
Show Gist options
  • Save luisvonmuller/67c594bfbb869efd62a50b087ee97878 to your computer and use it in GitHub Desktop.
Save luisvonmuller/67c594bfbb869efd62a50b087ee97878 to your computer and use it in GitHub Desktop.
Linkedin = {
config: {
scrollDelay: 500,
actionDelay: 500,
nextPageDelay: 2000,
// set to -1 for no limit
maxRequests: -1,
totalRequestsSent: 0,
},
init: function (data, config) {
console.info("INFO: SCRIPT HAS BEEN INITIALIZATED!");
console.debug("DEBUG: scrolling to bottom in " + config.scrollDelay + " ms");
setTimeout(() => this.scrollBottom(data, config), config.actionDelay);
},
scrollBottom: function (data, config) {
window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' });
console.debug("DEBUG: scrolling to top in " + config.scrollDelay + " ms");
setTimeout(() => this.scrollTop(data, config), config.scrollDelay);
},
scrollTop: function (data, config) {
window.scrollTo({ top: 0, behavior: 'smooth' });
console.debug("DEBUG: inspecting elements in " + config.scrollDelay + " ms");
setTimeout(() => this.inspect(data, config), config.scrollDelay);
},
inspect: function (data, config) {
var totalRows = this.totalRows();
console.debug("DEBUG: total search results found on page are " + totalRows);
if (totalRows >= 0) {
this.compile(data, config);
} else {
console.warn("WARN: end of search results!");
this.complete(config);
}
},
compile: function (data, config) {
var elements = document.getElementsByClassName("search-result__action-button search-result__actions--primary artdeco-button artdeco-button--default artdeco-button--2 artdeco-button--secondary");
data.pageButtons = [...elements].filter(function (element) {
return element.textContent.trim() === "Connect";
});
if (!data.pageButtons || data.pageButtons.length === 0) {
console.warn("ERROR: no connect buttons found on page!");
console.info("INFO: moving to next page...");
setTimeout(() => { this.nextPage(config) }, config.nextPageDelay);
} else {
data.pageButtonTotal = data.pageButtons.length;
console.info("INFO: " + data.pageButtonTotal + " connect buttons found");
data.pageButtonIndex = 0;
console.debug("DEBUG: starting to send invites in " + config.actionDelay + " ms");
setTimeout(() => { this.sendInvites(data, config) }, config.actionDelay);
}
},
sendInvites: function (data, config) {
console.debug("remaining requests " + config.maxRequests);
if (config.maxRequests == 0) {
console.info("INFO: max requests reached for the script run!");
this.complete(config);
} else {
console.debug('DEBUG: sending Connection invitation to ' + (data.pageButtonIndex + 1) + ' out of ' + data.pageButtonTotal);
var button = data.pageButtons[data.pageButtonIndex];
button.click();
console.debug("DEBUG: clicking done in popup, if present, in " + config.actionDelay + " ms");
setTimeout(() => this.clickDone(data, config), config.actionDelay);
}
},
clickDone: function (data, config) {
var buttons = document.querySelectorAll('button');
var doneButton = Array.prototype.filter.call(buttons, function (el) {
return el.textContent.trim() === 'Done';
});
// Click the first done button
if (doneButton && doneButton[0]) {
console.debug("DEBUG: clicking done button to close popup");
doneButton[0].click();
} else {
console.debug("DEBUG: done button not found, clicking close on the popup in " + config.actionDelay);
}
setTimeout(() => this.clickClose(data, config), config.actionDelay);
},
clickClose: async function (data, config) {
var WithdrawButton = document.getElementsByClassName('ml1 artdeco-button artdeco-button--3 artdeco-button--primary ember-view');
await new Promise(r => setTimeout(r, 1000));
if (WithdrawButton.length > 0) {
WithdrawButton[0].click();
console.info('INFO: invite sent to ' + (data.pageButtonIndex + 1) + ' out of ' + data.pageButtonTotal);
config.maxRequests--;
config.totalRequestsSent++;
}
if (data.pageButtonIndex === (data.pageButtonTotal - 1)) {
console.debug("DEBUG: all connections for the page done, going to next page in " + config.actionDelay + " ms");
setTimeout(() => this.nextPage(config), config.actionDelay);
} else {
data.pageButtonIndex++;
console.debug("DEBUG: sending next invite in " + config.actionDelay + " ms");
setTimeout(() => this.sendInvites(data, config), config.actionDelay);
}
},
nextPage: function (config) {
var pagerButton = document.getElementsByClassName('artdeco-pagination__button--next');
if (!pagerButton || pagerButton.length === 0) {
console.info("INFO: no next page button found!");
return this.complete(config);
}
console.info("INFO: Going to next page...");
pagerButton[0].children[0].click();
setTimeout(() => this.init({}, config), config.nextPageDelay);
},
complete: function (config) {
console.info('INFO: script completed after sending ' + config.totalRequestsSent + ' connection requests');
},
totalRows: function () {
var search_results = document.getElementsByClassName('search-result');
if (search_results && search_results.length != 0) {
return search_results.length;
} else {
return 0;
}
}
}
Linkedin.init({}, Linkedin.config);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment