-
-
Save gabrielribeiro/7bd97e33b1984ff58a24 to your computer and use it in GitHub Desktop.
A phantomjs script to:a) Login to Linkedin.comb) Scrape all your contactsc) Visit all the contactsd) Automatically endorse all contacts' skills.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var auth = { | |
user: "USERNAME", | |
pass: "PASSWORD" | |
}; | |
function ParallelRunner (list, func, runners) { | |
function createSlots(runners) { | |
var slots = []; | |
for (var i = 0; i < runners; i++) { | |
slots.push( objReturner() ); | |
} | |
return slots; | |
} | |
function objReturner() { | |
return { free: true }; | |
} | |
function toArray(obj) { | |
return Array.prototype.slice.call(obj); | |
} | |
function bind(scope, fn /*, variadic args to curry */ ) { | |
var args = Array.prototype.slice.call(arguments, 2); | |
return function() { | |
return fn.apply(scope, args.concat(toArray(arguments))); | |
}; | |
} | |
function _next() { | |
var slot = this.getFreeSlot(); | |
if (slot !== null) { | |
slot.free = false; | |
var cb = function() { | |
slot.free = true; | |
}; | |
if (list.length !== 0) { | |
var next = list.pop(); | |
func(next, cb); | |
} else {} | |
} | |
} | |
this.start = function() { | |
interval = setInterval(this.next, 100); | |
}; | |
this.stop = function(){ | |
clearInterval(interval); | |
}; | |
function _getFreeSlot() { | |
for (var i = slots.length - 1; i >= 0; i--) { | |
if (slots[i].free) { | |
return slots[i]; | |
} | |
} | |
return null; | |
} | |
runners = runners || 3; | |
var slots = createSlots(runners), | |
interval; | |
// Something weird happens with objects returned by PhantomJS page.evaluate() | |
// Fix em by rebuilding the objects. ONLY WORKS w/ SIMPLE OBJECTS!!! | |
list = JSON.parse(JSON.stringify(list)); | |
this.next = bind(this, _next); | |
this.getFreeSlot = bind(this, _getFreeSlot); | |
} | |
function visitAndEndorse(name, cb) { | |
var np = require("webpage").create(); | |
np.onConsoleMessage = function(msg) { | |
console.log(msg); | |
}; | |
np.open(name.href, function(status) { | |
if (status === "success") { | |
np.evaluate(function() { | |
function click(el) { | |
var ev = document.createEvent("MouseEvent"); | |
ev.initMouseEvent( | |
"click", | |
true /* bubble */ , true /* cancelable */ , | |
window, null, | |
0, 0, 0, 0, /* coordinates */ | |
false, false, false, false, /* modifier keys */ | |
0 /*left*/ , null | |
); | |
el.dispatchEvent(ev); | |
} | |
var name = document.querySelector(".full-name"); | |
if (name !== null) { | |
console.log("Visited " + name.textContent); | |
} | |
setTimeout(function() { | |
var endorse = document.querySelector(".btn-action.endorse-skills"); | |
if (endorse !== null) { | |
console.log("Endorsed " + name.textContent); | |
click(endorse); | |
} else { | |
console.log("No endorse"); | |
} | |
}, 3000); | |
}); | |
window.setTimeout(function() { | |
np.close(); | |
cb(); | |
}, 6000); | |
} | |
}); | |
} | |
var page = require("webpage").create(); | |
var names = []; | |
page.onConsoleMessage = function(msg) { | |
console.log(msg); | |
}; | |
page.open("http://www.linkedin.com/people/connections", function(status) { | |
if (status === "success") { | |
page.evaluate(function() { | |
function click(el) { | |
var ev = document.createEvent("MouseEvent"); | |
ev.initMouseEvent( | |
"click", | |
true /* bubble */ , true /* cancelable */ , | |
window, null, | |
0, 0, 0, 0, /* coordinates */ | |
false, false, false, false, /* modifier keys */ | |
0 /*left*/ , null | |
); | |
el.dispatchEvent(ev); | |
} | |
var button = document.querySelector("#uno-reg-join > div > div > div > div.content-container > div.reg-content-wrapper.single > div > div > p > a"); | |
click(button); | |
}); | |
window.setTimeout(function() { | |
page.evaluate(function(auth) { | |
document.querySelector("#session_key-login").value = auth.user; | |
document.querySelector("#session_password-login").value = auth.pass; | |
document.querySelector("#login").submit(); | |
console.log("Login submitted!"); | |
}, auth); | |
}, 1400); | |
window.setTimeout(function() { | |
console.log("Get names"); | |
names = page.evaluate(function() { | |
var all = document.querySelectorAll(".conx-list li"); | |
var list = []; | |
for (var i = all.length - 1; i >= 0; i--) { | |
var item = all[i]; | |
if (item.hasOwnProperty("id")) { | |
var nameInputs = item.getElementsByTagName("input"); | |
if (nameInputs.length > 0) { | |
console.log(nameInputs[0].value, item.id); | |
list.push({ | |
name: nameInputs[0].value, | |
href: "http://www.linkedin.com/profile/view?id=" + item.id | |
}); | |
} | |
} | |
} | |
console.log("Got " + list.length + " names"); | |
return list; | |
}); | |
var a = new ParallelRunner(names, visitAndEndorse, 3); | |
a.start(); | |
}, 6000); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code is also not running.
Request returned 0
https://static.licdn.com/scds/concat/common/js?h=7vr4nuab43rzvy2pgq7yvvxjk-9qa4rfxekcw3lt2c06h7p0kmf:1 in logError
Login submitted!
TypeError: undefined is not an object (evaluating 'this.headerView.sortDropdownView.updateSortDisplay')
Get names
Got 0 names