Skip to content

Instantly share code, notes, and snippets.

@mitchbne
Last active August 1, 2020 02:49
Show Gist options
  • Save mitchbne/8c3be21684d085776b9a77839368e56b to your computer and use it in GitHub Desktop.
Save mitchbne/8c3be21684d085776b9a77839368e56b to your computer and use it in GitHub Desktop.
var getConnectButtonsOnPage = () => {
var buttons = document.querySelectorAll("button.search-result__action-button")
var searchText = "Connect";
var found = []
buttons.forEach((button) => {
if (button.textContent.trim() == searchText) { found.push(button) }
})
return found
}
/**
* Get name of the connection
* @param modalTitle expected to be in the format 'Invite {{ name }} to connect'
*/
var getConnectionName = (modalTitle) => {
if (!modalTitle) { return null } // could optionally change this to 'there' if you like in the case of 'Hey {{ name }}'
return modalTitle.split(/Invite\s(.*)\sto\sconnect/)[1].split(" ")[0]
}
var startMakingConnections = () => {
var count = 0
var pointer = 0
var connectButtons;
var minutes = 60 * 1000
var seconds = 1000
// If you would like to send an optional message simply type your message into the quotation marks below.
// Similar to most String formatting in code, you can use '\n' to break the text and move it to a new line.
// To template in a connection's name, you can use {{ name }} or {{name}} in the middle of the message.
// Here's an example of what you can do with it:
// var optionalMessage = "Hey {{ name }}, I'd love to connect with you.\n\nMitch"
// By default empty quotations (i.e. "" ) will be treated as if there is no message, and will just send the invitation without a message.
var optionalMessage = "Hey {{ name }}, I'd love to connect with you.\n\nMitch"
var makeConnections = () => {
// Handle when page is just loaded.
if (pointer === 0){ connectButtons = getConnectButtonsOnPage() }
// Make sure we don't get throttled by rate limitting.
if (count > 50){
// Send only 50 requests every 30 minutes.
setTimeout(() => { count = 0; makeConnections() }, 30 * minutes)
} else {
// We wait 2 seconds here before making a new connection to give the
// invite modal time to open, have an invitation be sent and move to the
// next 'Connect' button in the list.
setTimeout(() => {
if (pointer < connectButtons.length){
makeConnection(connectButtons[pointer])
count += 1
pointer += 1
makeConnections()
} else {
count += 1
pointer = 0
document.querySelector(".artdeco-pagination__button.artdeco-pagination__button--next.artdeco-button.artdeco-button--muted.artdeco-button--icon-right.artdeco-button--1.artdeco-button--tertiary.ember-view").click() // Click next
setTimeout(() => {
makeConnections()
}, 10 * seconds) // Wait 10s after page load
}
}, Boolean(optionalMessage) ? 3 * seconds : 2 * seconds);
}
}
makeConnections()
var makeConnection = (el) => {
el.click() // Click the connect button
setTimeout(() => {
// Wait 1s for the modal to appear
// If a value is set for optionalMessage, send a note. Else, simply send the invitation as is.
if (Boolean(optionalMessage)){
var sendNoteButton = document.querySelector("#artdeco-modal-outlet button[aria-label='Add a note'")
sendNoteButton.click()
setTimeout(() => {
var modalTitle = document.querySelector("h2#send-invite-modal").innerText
var name = getConnectionName(modalTitle)
var textareaField = document.querySelector("#artdeco-modal-outlet #custom-message")
var connectionName = document.querySelector("h2#send-invite-modal").innerText
// The following lines will change 'Hey {{name}},' to 'Hey,' if no name can be found in the modal title.
let message = optionalMessage
message = name ? optionalMessage.replace("{{name}}", name) : optionalMessage.replace(" {{name}}", "")
message = name ? optionalMessage.replace("{{ name }}", name) : optionalMessage.replace(" {{ name }}", "")
textareaField.value = message
setTimeout(() => {
var doneButton = document.querySelector("button[aria-label='Done']")
doneButton.classList = Array.from(doneButton.classList).filter(c => c !== "artdeco-button--disabled").join(" ")
doneButton.disabled = false
doneButton.click()
}, 1 * seconds)
}, 200)
} else {
var sendNowButton = document.querySelector("#artdeco-modal-outlet button[aria-label='Send now']")
sendNowButton.click()
}
}, 1 * seconds);
}
}
startMakingConnections()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment