From 6e0f034eab324be87c993127dca1b821c28c2f1b Mon Sep 17 00:00:00 2001 From: bill-auger Date: Tue, 16 Jan 2018 02:15:31 -0500 Subject: stash --- post_fsd_wiki.phantomjs | 102 ++++++++++++++++++++++++------------------------ 1 file changed, 50 insertions(+), 52 deletions(-) diff --git a/post_fsd_wiki.phantomjs b/post_fsd_wiki.phantomjs index af8e5c8..a9a7a33 100644 --- a/post_fsd_wiki.phantomjs +++ b/post_fsd_wiki.phantomjs @@ -1,5 +1,3 @@ -console.log("in") ; - var WIKI_BASE_URL = 'http://localhost/mediawiki/index.php' ; // var WIKI_BASE_URL = 'https://directory.fsf.org/wiki?title=' ; // var LOGIN_URL = WIKI_URL + '/index.php?title=Special:Userlogin&action=submitlogin' ; @@ -24,7 +22,7 @@ var IsLoading = false ; var ShouldQuit = false ; var StepN = 0 ; -if (WIKI_LOGIN == '' || WIKI_PASS == '') { console.log(USAGE_MSG) ; phantom.exit() ; } +if (WIKI_LOGIN == '' || WIKI_PASS == '') { DBG(USAGE_MSG) ; phantom.exit() ; } var Steps = @@ -37,76 +35,84 @@ var Steps = function login() { -console.log("login() in") ; +DBG("in") ; // Page.settings.userAgent = 'SpecialAgent'; OpenUrl(LOGIN_URL , function() { -console.log("login() open ok") ; -// console.log("Page.content=" + Page.content) ; +DBG("login() open in") ; +// console.log("login() Page.content=" + Page.content) ; - Page.evaluate(function(login_input_id , pass_input_id , submit_input_id , - wiki_login , wiki_pass ) + QuitOnErr(Page.evaluate(function(login_input_id , pass_input_id , submit_input_id , + wiki_login , wiki_pass ) { +console.log("login() eval in" + login_input_id) ; + var login_input = document.getElementById(login_input_id ) ; var pass_input = document.getElementById(pass_input_id ) ; var submit_input = document.getElementById(submit_input_id) ; +console.log("login_input=" + ((!!login_input) ? login_input : 'NFG')) ; +console.log("submit_input=" + ((!!submit_input) ? submit_input : 'NFG')) ; + if (login_input === 'undefined' || login_input === 'undefined' || submit_input === 'undefined' || submit_input.form === 'undefined' ) - { console.log("ERROR: invalid login page") ; ShouldQuit = true ; return ; } + return "invalid login page" ; login_input.value = wiki_login ; pass_input.value = wiki_pass ; -console.log("login_input.value=" + login_input.value) ; -console.log("pass_input.value=" + pass_input.value) ; -console.log("submit_input.value=" + submit_input.value) ; +console.log("login() login_input.value=" + login_input.value) ; +console.log("login() pass_input.value=" + pass_input.value) ; +console.log("login() submit_input.value=" + submit_input.value) ; submit_input.form.submit() ; IsLoading = true ; -console.log("login() open out") ; +console.log("login() eval out") ; + } , LOGIN_IMPUT_ID , PASS_IMPUT_ID , SUBMIT_IMPUT_ID , WIKI_LOGIN , WIKI_PASS)) ; - } , LOGIN_IMPUT_ID , PASS_IMPUT_ID , SUBMIT_IMPUT_ID , WIKI_LOGIN , WIKI_PASS) ; +DBG("login() open out") ; }) ; - -console.log("login() out") ; +DBG("out") ; } , function verifyLogin() { -console.log("verifyLogin() in") ; +DBG("in") ; - Page.evaluate(function(username_li_id , wiki_login) + QuitOnErr(Page.evaluate(function(username_li_id , wiki_login) { var username_li = document.getElementById(username_li_id) ; console.log("verifyLogin() username_li.textContent=" + username_li.textContent ) ; - if (username_li === 'undefined' || username_li.textContent != wiki_login) - { console.log("ERROR: login failed") ; ShouldQuit = true ; } - } , USERNAME_LI_ID , WIKI_LOGIN) ; + if (username_li === 'undefined' || username_li.textContent.lowercase != wiki_login.lowercase) + return "login failed" ; + } , USERNAME_LI_ID , WIKI_LOGIN)) ; +DBG("out") ; } , function fetchEditPage() { -console.log("fetchEditPage() in") ; +DBG("in") ; var page_title = 'New Page' ; OpenUrl(EDIT_URL.replace(TITLE_URL_REGEX , page_title) , function() { - Page.evaluate(function(content_input_id , edit_form_id , content_text) + QuitOnErr(Page.evaluate(function(content_input_id , edit_form_id , content_text) { var content_input = document.getElementById(content_input_id) ; var edit_form = document.getElementById(edit_form_id ) ; -console.log("(edit_form == content_input.form)=" + (edit_form == content_input.form)) ; +console.log("(fetchEditPage() edit_form == content_input.form)=" + (edit_form == content_input.form)) ; console.log("fetchEditPage() content_input.textContent=" + content_input.textContent ) ; - if (content_input === 'undefined') { console.log("ERROR: invalid edit page") ; ShouldQuit = true ; } - } , CONTENT_INPUT_ID , EDIT_FORM_ID , content_text) ; + if (content_input === 'undefined') return "invalid edit page" ; + } , CONTENT_INPUT_ID , EDIT_FORM_ID , content_text)) ; }) ; + +DBG("out") ; } ] ; @@ -117,12 +123,12 @@ var MainIvl = setInterval(function() { if (IsLoading) return ; -console.log("test"); +DBG("test"); if (typeof Steps[StepN] !== 'function') ShouldQuit = true ; - if (ShouldQuit) { clearInterval(MainIvl) ; phantom.exit() ; } - else { console.log("Step " + StepN + ": " + Steps[StepN].name) ; Steps[StepN++]() ; } + if (ShouldQuit) { clearInterval(MainIvl) ; phantom.exit() ; } + else { LOG("Step " + StepN + ": " + Steps[StepN].name) ; Steps[StepN++]() ; } }) ; @@ -132,38 +138,30 @@ function OpenUrl(url , callback) { Page.open(LOGIN_URL , function(status) { - if (status != 'success') { console.log("ERROR: status: " + status) ; ShouldQuit = true ; } - else { callback() ; } + if (status != 'success') { ERR("status: " + status) ; ShouldQuit = true ; } + else { callback() ; } }) ; } +function QuitOnErr(err) { if (!!err) { ERR(err) ; ShouldQuit = true ; } } + /* event hendlers */ -Page.onLoadStarted = function() { IsLoading = true ; - console.log("page.onLoadStarted"); - printArgs.apply(this, arguments); -} ; +Page.onLoadStarted = function() { IsLoading = true ; ARGS.apply("Page.onLoadStarted" , arguments) ; } ; -Page.onLoadFinished = function() { IsLoading = false ; - console.log("page.onLoadFinished"); - console.log("Page.url=" + Page.url) ; - printArgs.apply(this, arguments); -} ; +Page.onLoadFinished = function() { IsLoading = false ; ARGS.apply("Page.onLoadFinished " + Page.url , arguments) ; } ; -Page.onUrlChanged = function() { - console.log("page.onUrlChanged"); - printArgs.apply(this, arguments); -} ; +Page.onUrlChanged = function() { ARGS.apply("Page.onUrlChanged" , arguments) ; } ; -Page.onConsoleMessage = function(msg) { console.log(msg) ; } ; +/* logging */ +Page.onConsoleMessage = function(msg) { DBG(msg) ; } ; -function printArgs() { - var i, ilen; - for (i = 0, ilen = arguments.length; i < ilen; ++i) { - console.log(" arguments[" + i + "] = " + JSON.stringify(arguments[i])); - } - console.log(""); -} +function LOG(log , color) { console.log((color || '\033[01;34m') + log + '\033[00m' ) ; } +function ERR(err) { LOG("ERROR: " + err , '\033[00;31m') ; } +function DBG(dbg) { var fn_name = (!!DBG.caller.name) ? DBG.caller.name + "() " : '' ; + LOG("DEBUG: " + fn_name + dbg , '\033[00;32m') ; } + +function ARGS() { LOG("EVENT: " + this) ; for (arg in arguments) LOG(" arg: " + JSON.stringify(arguments[arg])) ; } -- cgit v1.2.2