Skip to content

Instantly share code, notes, and snippets.

@maksii
Last active December 19, 2020 16:54
Show Gist options
  • 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);
@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