Permalink
Browse files

first demoable version of browser automation for facebook!

  • Loading branch information...
1 parent 8cc869d commit da3d7b6ddddcbbe892091ff811c3952fdd929406 @psawaya psawaya committed Mar 12, 2012
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -59,6 +59,18 @@ AutomationHelpers = function() {
callback_id: saveCallback(callback)
});
},
+ finishAutomation: function(workerID) {
+ self.postMessage({
+ type: 'finish_automation',
+ worker_id: workerID
+ });
+ },
+ registerError: function(error) {
+ self.postMessage({
+ type: 'raise_error',
+ error: error
+ });
+ },
registerWorker: function(id, func) {
self.postMessage({
type: 'register_worker',
@@ -88,6 +100,7 @@ AutomationHelpers = function() {
});
},
simulateClick: function(elements) {
+ if (!elements) return;
if (!elements.length) elements = [elements];
for (var elemIdx = 0; elemIdx < elements.length; elemIdx++) {
var evt = window.document.createEvent('MouseEvents');
@@ -27,7 +27,7 @@ function automate() {
return function() {
$(selector).unbind('DOMSubtreeModified.watchdogautomator');
if (ntimes == 1) {
- console.log('dom tree modified enough times');
+ // console.log('dom tree modified enough times');
doNextScriptStep();
$(selector).unbind('DOMSubtreeModified.watchdogautomator');
}
@@ -45,7 +45,7 @@ function automate() {
function runScript(script) {
if (script.timeout) {
- console.log('timeout ' + script.timeout);
+ // console.log('timeout ' + script.timeout);
setTimeout(function() {
delete script['timeout'];
runScript(script);
@@ -72,8 +72,7 @@ function automate() {
urlToAssert = eval(script.code);
if (unsafeWindow.location != urlToAssert) {
- unsafeWindow.location = urlToAssert;
- console.log ("setting location to " + urlToAssert);
+ unsafeWindow.location = urlToAssert;
}
else {
// FIXME: If the assertion succeeds, we need to manually advance the script step. Should clean this up.
@@ -85,7 +84,7 @@ function automate() {
break;
case 'simulate_click':
- console.log("simulating click on selector: " + script.target + " at " + (new Date()));
+ // console.log("simulating click on selector: " + script.target + " at " + (new Date()));
var targetElems = eval(script.target).get();
for (var elem in targetElems) {
@@ -1,9 +1,7 @@
AutomationHelpers.registerWorker('grabUserPage', function () {
$(document).ready(function() {
if ($('.UIPage_LoggedOut').length > 0) {
- self.postMessage({
- error: 'Not logged in!'
- });
+ AutomationHelpers.registerError('Not logged in to Facebook!');
return;
}
@@ -12,8 +10,9 @@ AutomationHelpers.registerWorker('grabUserPage', function () {
var facebookInfoURL = $('li.tinyman > a').attr('href').split('?')[0] + '/info';
- AutomationHelpers.runWorker('getPrivacySettings', "http://www.facebook.com/settings/?tab=privacy", true);
- AutomationHelpers.runWorker('getUserInfo', facebookInfoURL, true);
+ AutomationHelpers.runWorker('getPrivacySettings', "http://www.facebook.com/settings/?tab=privacy");
+ AutomationHelpers.runWorker('getUserInfo', facebookInfoURL);
+ AutomationHelpers.finishAutomation('grabUserPage');
});
});
@@ -32,12 +31,10 @@ AutomationHelpers.registerWorker('getPrivacySettings', function() {
firstSettingTimer = setInterval(function() {
// TODO: timeout when this doesn't work after enough times
if ($('.pop_dialog .uiButtonText:visible').length == 5) {
- console.log('.pop_dialog:visible');
clearInterval(firstSettingTimer);
var settingsOnDialog1 = $('.pop_dialog .uiButtonText:visible').get();
- console.log(settingsOnDialog1);
- console.log(JSON.stringify(settingsOnDialog1));
+
const settingsTable = [
"Who can find you",
"Who can send friend requests",
@@ -65,6 +62,7 @@ AutomationHelpers.registerWorker('getPrivacySettings', function() {
AutomationHelpers.returnValue("Maximum timeline visibility", $('.pop_dialog .uiButtonText:visible').text());
AutomationHelpers.returnValue("Tag Suggestions", $('#tag_suggestion_setting').text());
AutomationHelpers.returnValue("Friends can check you in", $('#checkin_tags_setting').text());
+ AutomationHelpers.finishAutomation('getPrivacySettings');
}
});
}
@@ -96,9 +94,14 @@ AutomationHelpers.registerWorker('setPrivacySettings', function() {
AutomationHelpers.registerWorker('getUserInfo', function() {
- $(document).ready(function() {
-
-
+ $(document).ready(function() {
+ // Check if the user has timeline
+ if ($('.fbTimelineViewingSelf').length == 0) {
+ AutomationHelpers.registerError("You don't have Facebook's timeline profile. We can grab privacy settings, but not userinfo. :(");
+ AutomationHelpers.finishAutomation('getUserInfo');
+ return;
+ }
+
// Click every privacy edit button
AutomationHelpers.simulateClick($('.profileEditButton').get());
@@ -107,6 +110,7 @@ AutomationHelpers.registerWorker('getUserInfo', function() {
}
function collectData() {
+
var contactElems = $('#edit_contact_info').find('.checked');
contacts = contactElems.map(mapToDataLabel);
for (var contactX = 0; contactX < contacts.length; contactX++) {
@@ -130,19 +134,17 @@ AutomationHelpers.registerWorker('getUserInfo', function() {
var relationshipStatus = $('#edit_relationship_info').find('.checked')[0];
AutomationHelpers.returnValue('Relationship status',$(relationshipStatus).attr('data-label'));
-
-
var quotesSelector = $('[data-contextselector="#pagelet_quotes .uiHeader"]').find('.checked').get()[0];
- AutomationHelpers.returnValue('Favorite quotes',$(quotesSelector).attr('data-label'));
+ AutomationHelpers.returnValue('Favorite quotes',$(quotesSelector).attr('data-label'));
+
+ AutomationHelpers.finishAutomation('getUserInfo');
}
- // Wait for half a second after the last object loads.
-
+ // Wait for half a second after the last object loads.
var waitForLoad = null;
$('#timeline_tab_content').bind('DOMSubtreeModified.watchdog', function() {
- console.log('DOM update, newtimeout');
unsafeWindow.clearTimeout(waitForLoad);
waitForLoad = unsafeWindow.setTimeout(collectData,3000);
@@ -151,6 +153,6 @@ AutomationHelpers.registerWorker('getUserInfo', function() {
});
});
-AutomationHelpers.runWorker('setPrivacySettings', "http://www.facebook.com/settings/?tab=privacy", true);
+// AutomationHelpers.runWorker('setPrivacySettings', "http://www.facebook.com/settings/?tab=privacy", true);
-// AutomationHelpers.runWorker('grabUserPage', "http://www.facebook.com", true);
+AutomationHelpers.runWorker('grabUserPage', "http://www.facebook.com");
@@ -9,7 +9,5 @@ $(document).ready(function() {
newDiv.css('display','block');
$('#privacyProperties').append(newDiv);
-
- console.log("Pane name : " + msg.property + " pane value: " + msg.value);
});
});
View
@@ -0,0 +1,10 @@
+self.port.on('populateSettings', function(results) {
+ document.getElementById('automationResults').style['display'] = 'block';
+ document.getElementById('automationResults').value = JSON.stringify(results);
+});
+
+document.getElementById('startLink').addEventListener('click', function() {
+ self.postMessage({
+ type: 'start_automate'
+ });
+});
View
@@ -0,0 +1,22 @@
+<html>
+<head>
+ <style>
+ body {
+ background-color:white;
+ color:black;
+ }
+
+ #automationResults {
+ display:none;
+ }
+ </style>
+</head>
+<body>
+ <div>
+ <a href="#" id="startLink">Start</a> collecting Facebook privacy settings.
+ </div>
+ <div>
+ <textarea rows=13 cols=32 id="automationResults"></textarea>
+ </div>
+</body>
+</html>
View
@@ -5,45 +5,61 @@ const pageWorker = require("page-worker");
const tabs = require('tabs');
const url = require("url");
+const main = require("main");
+
var automationScript = require('automation_script')['automationScript'];
var automatorPane = require('automator_pane_manager').automatorPane;
const testSettings = {
"Who can find you": "Everyone"
};
-function testAutomate() {
+var workersRunning = [];
+
+var returnedValues = {};
+
+exports['runAutomation'] = function() {
var automationWorkers = {};
+ returnedValues = {};
function workerMessageHandler(msg) {
if (msg.type) {
switch(msg.type) {
+ case "raise_error":
+ main.raiseError(msg.error);
+ break;
case "register_worker":
automationWorkers[msg.id] = msg.func.toString();
break;
case "run_worker":
spawnWorkerForPage(msg.url, msg.id, msg.visual);
+ workersRunning.push(msg.id);
break;
case "return_value":
- automatorPane.addProperty(msg.key,msg.value);
+ returnedValues[msg.key] = msg.value;
break;
case "fetch_setting":
- console.log("fetch_setting: " + JSON.stringify(msg));
this.port.emit("fetch_setting_callback",{
callback_id: msg.callback_id,
value: testSettings[msg.setting_name]
});
break;
+ case "finish_automation":
+ var workerIdx = workersRunning.indexOf(msg.worker_id);
+ if (workerIdx != -1)
+ workersRunning.splice(workerIdx,1);
+ if (workersRunning.length == 0)
+ main.finishAutomation(returnedValues);
+ break;
}
}
if (msg.error) {
automatorPane.showError(msg.error);
return;
}
- if (msg.url)
- console.log('URL: ' + msg.url);
- console.log("this = " + this)
+ // if (msg.url)
+ // console.log('URL: ' + msg.url);
}
function spawnWorkerForPage(url, workerScript, visual) {
@@ -80,29 +96,4 @@ function testAutomate() {
// Start at "dummy" page, Google in this case.
spawnWorkerForPage("http://google.com");
- automatorPane.show();
-}
-
-testAutomate();
-
-Automator = function() {
-
-}
-
-Automator.prototype.runScript = function(script) {
-
-};
-
-
-
-
-
-
-
-// function onContent(domObj) {
-// let window = domObj.defaultView;
-// if (window == null) return;
-// let hostname = window.location.hostname;
-// }
-//
-// observers.add('document-element-inserted', onContent);
+}
View
@@ -1 +1,45 @@
-require('automation');
+const dataDir = require("self").data;
+const notifications = require("notifications");
+const panel = require("panel");
+const widgets = require("widget");
+
+const automation = require("automation");
+
+var automatePanel = panel.Panel({
+ contentURL: dataDir.url("panel.html"),
+ contentScriptFile: [dataDir.url("js/panel.js")],
+ onMessage: function(msg) {
+ if (msg.type == 'start_automate') {
+ notifications.notify({
+ title: "Watchdog Automator",
+ text: "Starting Facebook settings collection"
+ });
+ automation.runAutomation();
+ }
+ }
+});
+
+var widget = widgets.Widget({ id: "privacy-watchdog-automate-link",
+ label: "Watchdog Facebook Automator",
+ contentURL: dataDir.url("gear.png"),
+ panel: automatePanel
+ });
+
+exports['raiseError'] = function(errorStr) {
+ notifications.notify({
+ title: "Watchdog Automator",
+ iconURL: dataDir.url('error.png'),
+ text: errorStr
+ });
+};
+
+exports['finishAutomation'] = function(results) {
+ automatePanel.port.emit('populateSettings',results);
+ notifications.notify({
+ title: "Watchdog Automator",
+ text: "Facebook settings collected! Click to view.",
+ onClick: function() {
+ automatePanel.show();
+ }
+ });
+};

0 comments on commit da3d7b6

Please sign in to comment.