Skip to content

Instantly share code, notes, and snippets.

@maksii
Last active December 19, 2020 16:54
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save maksii/e028dc8de7d42f12305911e53679d807 to your computer and use it in GitHub Desktop.
Save maksii/e028dc8de7d42f12305911e53679d807 to your computer and use it in GitHub Desktop.
Script to automatically add connections from "People You May Know" page
var inviter = {} || inviter;
inviter.userList = [];
inviter.className = 'button-secondary-small';
inviter.refresh = function () {
window.scrollTo(0, document.body.scrollHeight);
window.scrollTo(document.body.scrollHeight, 0);
window.scrollTo(0, document.body.scrollHeight);
};
inviter.initiate = function()
{
inviter.refresh();
var connectBtns = document.getElementsByClassName(inviter.className);
if (connectBtns == null) {var connectBtns = inviter.initiate();}
return connectBtns;
};
inviter.invite = function () {
var connectBtns = inviter.initiate();
var buttonLength = connectBtns.length;
for (var i = 0; i < buttonLength; i++) {
if (connectBtns != null && connectBtns[i] != null) {inviter.handleRepeat(connectBtns[i]);}
if (i == buttonLength - 1) {
console.log("done: " + i);
inviter.refresh();
}
}
};
inviter.handleRepeat = function(button)
{
var nameValue = button.children[1].textContent
var name = nameValue.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
if(inviter.arrayContains(name))
{
console.log("cancel");
var cancel = button.parentNode.parentNode.children[0];
cancel.click();
}
else
{
console.log("add");
inviter.userList.push(name);
button.click();
}
};
inviter.arrayContains = function(item)
{
return (inviter.userList.indexOf(item) > -1);
};
inviter.usersJson = {};
inviter.loadResult = function()
{
var retrievedObject = localStorage.getItem('inviterList');
var temp = JSON.stringify(retrievedObject);
inviter.userList = JSON.parse(temp);
};
inviter.saveResult = function()
{
inviter.usersJson = JSON.stringify(inviter.userList);
localStorage.setItem('inviterList', inviter.usersJson);
};
setInterval(function () { inviter.invite(); }, 5000);
@quangnhut123
Copy link

Hello thanks this script work for me but need to modify like this to easy for follow which user added.

var inviter = {} || inviter;
inviter.userList = [];
inviter.className = 'button-secondary-small';

inviter.refresh = function() {
window.scrollTo(0, document.body.scrollHeight);
window.scrollTo(document.body.scrollHeight, 0);
window.scrollTo(0, document.body.scrollHeight);
};

inviter.initiate = function() {
inviter.refresh();
var connectBtns = document.getElementsByClassName(inviter.className);
if (connectBtns == null) {
var connectBtns = inviter.initiate();
}
return connectBtns;
};

inviter.invite = function() {
var connectBtns = inviter.initiate();
var buttonLength = connectBtns.length;
for (var i = 0; i < buttonLength; i++) {
if (connectBtns != null && connectBtns[i] != null) {
inviter.handleRepeat(connectBtns[i]);
}
if (i == buttonLength - 1) {
console.log("Number of invitation sent out: " + i);
inviter.refresh();
}
}
};

inviter.handleRepeat = function(button) {
var nameValue = button.children[1].textContent
var name = nameValue.replace(/Invite|to connect|/gi, '').trim();
if (inviter.arrayContains(name)) {
console.log("Already sent invitation for " + name);
var cancel = button.parentNode.parentNode.children[0];
cancel.click();
} else {
console.log("Adding user: " + name);
inviter.userList.push(name);
button.click();
}
};

inviter.arrayContains = function(item) {
return (inviter.userList.indexOf(item) > -1);
};

inviter.usersJson = {};
inviter.loadResult = function() {
var retrievedObject = localStorage.getItem('inviterList');
var temp = JSON.stringify(retrievedObject);
inviter.userList = JSON.parse(temp);
};

inviter.saveResult = function() {
inviter.usersJson = JSON.stringify(inviter.userList);
localStorage.setItem('inviterList', inviter.usersJson);
};

setInterval(function() {
inviter.invite();
}, 5000);

@Tumas2
Copy link

Tumas2 commented Sep 4, 2017

//Modified the "Add CEO's only" code a bit cause I couldn't get it to work.
//Should run fine now:

// ! EDIT !
// I Have made it so you can search for an array of roles

// Write any role you want the person to have, example "CEO", "Investor"
// !! Don't forget to add comma efter every word !! //
var userRole = [
"CEO",
"Investor"
];

var inviter = {} || inviter;
inviter.userList = [];
inviter.className = 'button-secondary-small';
var addedPpl = 0;

inviter.refresh = function () {
window.scrollTo(0, document.body.scrollHeight);
window.scrollTo(document.body.scrollHeight, 0);
window.scrollTo(0, document.body.scrollHeight);
};

inviter.initiate = function()
{
inviter.refresh();
var connectBtns = document.getElementsByClassName(inviter.className);

if (connectBtns == null) {var connectBtns = inviter.initiate();}

return connectBtns;
};
inviter.invite = function () {
var connectBtns = inviter.initiate();
var buttonLength = connectBtns.length;
for (var i = 0; i < buttonLength; i++) {
if (connectBtns != null && connectBtns[i] != null) {inviter.handleRepeat(connectBtns[i]);}
if (i == buttonLength - 1) {
console.log("Added: " + addedPpl);
inviter.refresh();
}
}
};

inviter.handleRepeat = function(button)
{
var nameValue = button.children[1].textContent
var name = nameValue.replace(/^\s\s*/, '').replace(/\s\s*$/, '');

function hasRole(role){
for(var i = 0; i < role.length; i++) {
var position = button.parentNode.parentNode.children[1].children[1].children[0].children[3].textContent;
var formatedPosition = position.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
var hasRole = formatedPosition.indexOf(role[i]) == -1 ? false : true;
console.log('Has role: ' + role[i] + ' -> ' + hasRole);
if (hasRole) {
return hasRole;
}
}
return false;
}

if(inviter.arrayContains(name))
{
console.log("cancel");
var cancel = button.parentNode.parentNode.children[0];
cancel.click();
}
else if (hasRole(userRole) == false) {
console.log("cancel");
var cancel = button.parentNode.parentNode.children[0];
cancel.click();
}
else
{
console.log("add");
inviter.userList.push(name);
button.click();
addedPpl++;
}
};

inviter.arrayContains = function(item)
{
return (inviter.userList.indexOf(item) > -1);
};

inviter.usersJson = {};

inviter.loadResult = function()
{

var retrievedObject = localStorage.getItem('inviterList');
var temp = JSON.stringify(retrievedObject);
inviter.userList = JSON.parse(temp);
};

inviter.saveResult = function()
{
inviter.usersJson = JSON.stringify(inviter.userList);
localStorage.setItem('inviterList', inviter.usersJson);
};

setInterval(function () { inviter.invite(); }, 5000);

@dheerajbhaskar
Copy link

dheerajbhaskar commented Sep 24, 2017

updated: 'add' to 'added', 'cancel' to 'canceled'
added: skipping of invites (i.e. emailing people not yet on linkedin)
added: "skipped" logging to console

var inviter = {} || inviter;
inviter.userList = [];
inviter.className = 'button-secondary-small';

inviter.refresh = function () {
    window.scrollTo(0, document.body.scrollHeight);
    window.scrollTo(document.body.scrollHeight, 0);
    window.scrollTo(0, document.body.scrollHeight);
};

inviter.initiate = function()
{
    inviter.refresh();
    var connectBtns = document.getElementsByClassName(inviter.className);

    if (connectBtns == null) {var connectBtns = inviter.initiate();}
    
    return connectBtns;
};
inviter.invite = function () {
    var connectBtns = inviter.initiate();
    var buttonLength = connectBtns.length;
    for (var i = 0; i < buttonLength; i++) {

        if (connectBtns != null && connectBtns[i] != null) {inviter.handleRepeat(connectBtns[i]);}
            
        if (i == buttonLength - 1) {
            console.log("done: " + i);
            inviter.refresh();
        }
    }

};

inviter.handleRepeat = function(button)
{
    var nameValue = button.children[1].textContent
    var name = nameValue.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    if(inviter.arrayContains(name))
    {
         console.log("canceled");
         var cancel = button.parentNode.parentNode.children[0];
         cancel.click();
    }
    else if (button.textContent.indexOf("Connect")<0){
        console.log("skipped");
        inviter.userList.push(name); // it's likely that this person didn't join linkedin in the meantime, so we'll ignore them
        var cancel = button.parentNode.parentNode.children[0];
        cancel.click();
    }
    else {
        console.log("added");
        inviter.userList.push(name);
        button.click();
         
    }
};

inviter.arrayContains = function(item)
{
    return (inviter.userList.indexOf(item) > -1);
};

inviter.usersJson = {};

inviter.loadResult = function()
{
 
    var retrievedObject = localStorage.getItem('inviterList');
    var temp = JSON.stringify(retrievedObject);
    inviter.userList = JSON.parse(temp);
};

inviter.saveResult = function()
{
    inviter.usersJson = JSON.stringify(inviter.userList);
    localStorage.setItem('inviterList', inviter.usersJson);
};


setInterval(function () { inviter.invite(); }, 5000);

@dheerajbhaskar
Copy link

Massive performance increase (as on 2017-09-25)

fixes: re-querying already processed buttons // Massive performance increase

var inviter = {} || inviter;
inviter.userList = [];
inviter.className = 'button-secondary-small';

inviter.refresh = function () {
    window.scrollTo(0, document.body.scrollHeight);
    window.scrollTo(document.body.scrollHeight, 0);
    window.scrollTo(0, document.body.scrollHeight);
};

inviter.initiate = function()
{
    inviter.refresh();
    var connectBtns = $(".button-secondary-small:visible");

    if (connectBtns == null) {var connectBtns = inviter.initiate();}
    
    return connectBtns;
};
inviter.invite = function () {
    var connectBtns = inviter.initiate();
    var buttonLength = connectBtns.length;
    for (var i = 0; i < buttonLength; i++) {

        if (connectBtns != null && connectBtns[i] != null) {inviter.handleRepeat(connectBtns[i]);}
            
        if (i == buttonLength - 1) {
            console.log("done: " + i);
            inviter.refresh();
        }
    }

};

inviter.handleRepeat = function(button)
{
    var nameValue = button.children[1].textContent
    var name = nameValue.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    if(inviter.arrayContains(name))
    {
         console.log("canceled");
         var cancel = button.parentNode.parentNode.children[0];
         cancel.click();
    }
    else if (button.textContent.indexOf("Connect")<0){
        console.log("skipped");
        inviter.userList.push(name); // it's likely that this person didn't join linkedin in the meantime, so we'll ignore them
        var cancel = button.parentNode.parentNode.children[0];
        cancel.click();
    }
    else {
        console.log("added");
        inviter.userList.push(name);
        button.click();
         
    }
};

inviter.arrayContains = function(item)
{
    return (inviter.userList.indexOf(item) > -1);
};

inviter.usersJson = {};

inviter.loadResult = function()
{
 
    var retrievedObject = localStorage.getItem('inviterList');
    var temp = JSON.stringify(retrievedObject);
    inviter.userList = JSON.parse(temp);
};

inviter.saveResult = function()
{
    inviter.usersJson = JSON.stringify(inviter.userList);
    localStorage.setItem('inviterList', inviter.usersJson);
};


setInterval(function () { inviter.invite(); }, 5000);

@dheerajbhaskar
Copy link

dheerajbhaskar commented Sep 25, 2017

Also, looks like local storage is not used at all. My apologies if I'm mistaken.

If I reload the page and run this again, linkedin repeats invites and this script sends invites to the repeated ones as well. Right now, I'm not seeing any repeats in the same session (not sure if linkedin has stopped it or I'm not taking note of people properly)

Can you suggest how to use localstorage to preserve the inviterlist properly? Can you update the script with the same?

@000011111111
Copy link

I am at 23k in connections. @dheerajbhaskar, when I run your script I get the 400 error below. As a result, my connection request never passes through to the server. I went from being able to get 500-1000 new connections per day to 20-50?

b08bmcz73ndcu3s7cqqx9pgy7:1217 POST https://www.linkedin.com/voyager/api/growth/normInvitations

Are other users experiencing this?

@masterblub
Copy link

Hey Thank you very much! its an amazing script! But one thing is it some how possible to filter after location?
or to change the script that i search from persons from New York City for example and then to add all people there?
Because like this i get only people from the area I'm currently living in...

@maksii
Copy link
Author

maksii commented Oct 4, 2017

@dheerajbhaskar @Tumas2 @quangnhut123 Thanks for your input. That is currently not really maintained by me in any way, but I'll try to add your changes and fix "missed" localstorage logic.

@jopetersen
Copy link

jopetersen commented Nov 13, 2017

Working on fixing a few bugs that have come up within the past week. Trying to add the userRole back to the function (more helpful in my opinion). Code is as follows:

var userRole = [
"CEO",
"CIO"
];

var inviter = {} || inviter;
inviter.userList = [];
inviter.className = 'button-secondary-small';

inviter.refresh = function () {
    window.scrollTo(0, document.body.scrollHeight);
    window.scrollTo(document.body.scrollHeight, 0);
    window.scrollTo(0, document.body.scrollHeight);
};

inviter.initiate = function()
{
    inviter.refresh();
    var connectBtns = $(".button-secondary-small:visible");
// 

    if (connectBtns == null) {var connectBtns = inviter.initiate();}
    
    return connectBtns;
};
inviter.invite = function () {
    var connectBtns = inviter.initiate();
    var buttonLength = connectBtns.length;
    for (var i = 0; i < buttonLength; i++) {
        if (connectBtns != null && connectBtns[i] != null) {inviter.handleRepeat(connectBtns[i]);}
            //if there is a connect button and there is at least one that has not been pushed, repeat
        if (i == buttonLength - 1) {
            console.log("done: " + i);
            inviter.refresh();
        }
    }

};


inviter.handleRepeat = function(button)
{
    var nameValue = button.children[1].textContent
    var name = nameValue.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
	
	function hasRole(role){
	for(var i = 0; i < role.length; i++) {
		// cannot read children of undefined
		var position = button.parentNode.parentNode.children[1].children[1].children[0].children[3].textContent;
		var formatedPosition = position.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
		var hasRole = formatedPosition.indexOf(role[i]) == -1 ? false : true;
		console.log('Has role: ' + role[i] + ' -> ' + hasRole);
		if (hasRole) {
			return hasRole;
					}
				}
			return false;
	}
    if(inviter.arrayContains(name))
	
    {
         console.log("canceled");
         var cancel = button.parentNode.parentNode.children[0];
         cancel.click();
    }
	else if (hasRole(userRole) == false) {
		console.log("cancel");
		var cancel = button.parentNode.parentNode.children[0];
		cancel.click();
		}
    else if (button.textContent.indexOf("Connect")<0){
        console.log("skipped");
        inviter.userList.push(name); // it's likely that this person didn't join linkedin in the meantime, so we'll ignore them
        var cancel = button.parentNode.parentNode.children[0];
        cancel.click();
    }
    else {
        console.log("added");
        inviter.userList.push(name);
        button.click();
    }
};



inviter.arrayContains = function(item)
{
    return (inviter.userList.indexOf(item) > -1);
};

inviter.usersJson = {};

inviter.loadResult = function()
{
 
    var retrievedObject = localStorage.getItem('inviterList');
    var temp = JSON.stringify(retrievedObject);
    inviter.userList = JSON.parse(temp);
};

inviter.saveResult = function()
{
    inviter.usersJson = JSON.stringify(inviter.userList);
    localStorage.setItem('inviterList', inviter.usersJson);
};


setInterval(function () { inviter.invite(); }, 5000);`

When I try executing this, I get the following error:

VM288:49 Uncaught TypeError: Cannot read property 'children' of undefined
    at hasRole (<anonymous>:49:71)
    at Object.inviter.handleRepeat (<anonymous>:66:11)
    at Object.inviter.invite (<anonymous>:30:69)
    at <anonymous>:108:35

Any ideas as to how to fix it?

@ItsDanielHarris
Copy link

I wrote this script to automate send connection invites from search result pages.

@ItsDanielHarris
Copy link

ItsDanielHarris commented Nov 20, 2017

@000011111111 My script also has resulted in POST prevention as a result of this script (https://www.linkedin.com/voyager/api/growth/normInvitations). In addition, normal connect invitations do not work anymore.

@ItsDanielHarris
Copy link

Normal invitations seem to be working now.

@mydim
Copy link

mydim commented Nov 22, 2017

Daniel, nice work =)

@turnsource
Copy link

Nice, work! Loving the script, Daniel. Maybe check out mine here: http://www.turnsourceimaging.com/
Keep up the great work!

@ch01ca
Copy link

ch01ca commented Jan 22, 2018

@dheerajbhaskar could you please add position CEO PRESIDENT? Thanks!

@miguelmota
Copy link

Simple version

setInterval(() => {
    $('button[data-control-name="invite"]').each((i, el) => el.click())
    window.scrollTo(0, document.body.scrollHeight)
    window.scrollTo(document.body.scrollHeight, 0)
    window.scrollTo(0, document.body.scrollHeight)
}, 5e3)

run it on https://www.linkedin.com/mynetwork/

@thanhcs94
Copy link

@miguelmota It's simple but works like a champ :) Thanks!

@dheerajbhaskar
Copy link

@000011111111 not sure what this POST error is, I've been using this once in a while rather successfully. I plan to use this more frequently, will fix this sometime soon

@aero007 please check out jopetersen's post, he seems to have implemented what you're asking for

@malith1992
Copy link

malith1992 commented Jun 22, 2018

I have tried the original code by the author and it worked well for me. Then I wanted to make sure that this script adds only the people who have "CEO" word in the profile headline. Then I have tried the solutions mentioned above in the comment and non of them work. So I have modified the original code to archive me goal. ( Add the strings(lowercase) you want to be existed in the headline to the "setOfStrings" variable - line 6 )

    'var inviter = {} || inviter;
inviter.userList = [];
inviter.className = 'button-secondary-small';

//your strings here
var setOfStrings = ['ceo, '', 'founder',]; 

inviter.refresh = function () {
window.scrollTo(0, document.body.scrollHeight);
window.scrollTo(document.body.scrollHeight, 0);
window.scrollTo(0, document.body.scrollHeight);
};

inviter.initiate = function()
{
inviter.refresh();
var connectBtns = document.getElementsByClassName(inviter.className);

if (connectBtns == null) {var connectBtns = inviter.initiate();}

return connectBtns;
};
inviter.invite = function () {
var connectBtns = inviter.initiate();
var buttonLength = connectBtns.length;
for (var i = 0; i < buttonLength; i++) {

	if (connectBtns != null && connectBtns[i] != null) {inviter.handleRepeat(connectBtns[i]);}
		
	if (i == buttonLength - 1) {
		console.log("done: " + i);
		inviter.refresh();
	}
}

};

inviter.handleRepeat = function(button)
{
var nameValue = button.children[1].textContent
var name = nameValue.replace(/^\s\s*/, '').replace(/\s\s*$/, '');

var position = button.parentNode.parentNode.children[2].textContent;
var formatedPosition = position.replace(/^\s\s*/, '').replace(/\s\s*$/, '').toLowerCase();
console.log(formatedPosition);

var stringExists = false;

for(var i =0;i < setOfStrings.length; i++){
	if(formatedPosition.indexOf(setOfStrings[i]) != -1)
	stringExists = true;
}

if(inviter.arrayContains(name))
{
	 console.log("cancel");
	 var cancel = button.parentNode.parentNode.children[0];
	 cancel.click();
}
else
{
	if(stringExists == true)
	{
		console.log("add");
		inviter.userList.push(name);
		button.click();
	} 
	else
	{
		console.log("cancel");
		var cancel = button.parentNode.parentNode.children[0];
		cancel.click();
	}
}
};

inviter.arrayContains = function(item)
{
return (inviter.userList.indexOf(item) > -1);
};

inviter.usersJson = {};

inviter.loadResult = function()
{

var retrievedObject = localStorage.getItem('inviterList');
var temp = JSON.stringify(retrievedObject);
inviter.userList = JSON.parse(temp);

};

inviter.saveResult = function()
{
inviter.usersJson = JSON.stringify(inviter.userList);
localStorage.setItem('inviterList', inviter.usersJson);
};


setInterval(function () { inviter.invite(); }, 5000);`

@xampocode
Copy link

@miguelmota your script is amazing in the numbers but can you make one with a selection of keywords ?
obrigado

@rishumonga
Copy link

rishumonga commented Dec 30, 2018

Looks like LinkedIn changed something over the last couple of weeks and the scripts on this page, except the one from @miguelmota, do not seem to work anymore and that one doesn't let you select keywords. I hope that someone who knows this script well can fix this. Thanks, guys!

@ItayRosen
Copy link

Looks like LinkedIn changed something over the last couple of weeks and the scripts on this page, except the one from @miguelmota, do not seem to work anymore and that one doesn't let you select keywords. I hope that someone who knows this script well can fix this. Thanks, guys!

Try using this script.

@phpenterprise
Copy link

Hello developers, see this script solution.

Linkedin Auto Connect - Custom Search

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