summaryrefslogtreecommitdiff
path: root/post_fsd_wiki.phantomjs
blob: 97eaf1fe8f47035c99b2c7d38a3c8847afa4917e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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' ;
var LOGIN_URL         = WIKI_BASE_URL + '?title=Special:UserLogin' ;
var EDIT_URL          = WIKI_BASE_URL + '?title=_TITLE_&action=edit&redlink=1' ;
var TITLE_URL_REGEX   = '/_TITLE_/' ;
var USAGE_MSG         = "USAGE: phantomjs ./post_fsd_wiki.phantomjs <WIKI_LOGIN> <WIKI_PASS>" ;
var LOGIN_IMPUT_ID    = 'wpName1' ;
var PASS_IMPUT_ID     = 'wpPassword1' ;
var SUBMIT_IMPUT_ID   = 'wpLoginAttempt' ;
var USERNAME_LI_ID    = 'pt-userpage' ;
var CONTENT_INPUT_ID  = 'wpTextbox1' ;
var EDIT_FORM_ID      = 'editform' ;
var STEP_FUNCTION_KEY = 'step-finction' ;
var PAGELOAD_WAIT_KEY = 'wait-for-pageload' ;

var System     = require('system') ;
var Args       = System.args ;
var Page       = require('webpage').create() ;
var WIKI_LOGIN = Args[1] || '' ;
var WIKI_PASS  = Args[2] || '' ;
var Steps      = [] ;
var Step       = {} ;
var StepN      = 0 ;
var IsLoading  = false ;
var ShouldQuit = false ;
var MainIvl ;

if (WIKI_LOGIN == '' || WIKI_PASS == '') { DBG(USAGE_MSG) ; phantom.exit() ; }


/* steps */

function prepare()
{
  var title = 'a title' ;
  var page_title = JSON.encode(title.eplace(' ' , '_')) ;
}

function login()
{
DBG("in") ;
  // Page.settings.userAgent = 'SpecialAgent';

  OpenUrl(LOGIN_URL , function()
  {
DBG("login() open in") ;

    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) ;
      var submit_form  = submit_input.form ;

console.log("login_input=" + ((!!login_input) ? login_input : 'NFG')) ;
console.log("submit_input=" + ((!!submit_input) ? submit_input : 'NFG')) ;

      if (!login_input  || !login_input || !submit_input || !submit_form)
        return "invalid login page" ;

      login_input.value = wiki_login ;
      pass_input.value  = wiki_pass ;

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_form.submit() ;

console.log("login() eval out") ;
    } , LOGIN_IMPUT_ID , PASS_IMPUT_ID , SUBMIT_IMPUT_ID , WIKI_LOGIN , WIKI_PASS)) ;

DBG("login() open out") ;
  }) ;
DBG("out") ;
}

function verifyLogin()
{
DBG("in") ;
// console.log("verifyLogin() Page.content=" + Page.content) ;

  QuitOnErr(Page.evaluate(function(username_li_id , wiki_login)
  {
    var username_li = document.getElementById(username_li_id) ;

    if (!username_li || username_li.textContent.lowercase != wiki_login.lowercase)
      return "login failed" ;

    console.info("signed in as: " + username_li.textContent) ;
  } , USERNAME_LI_ID , WIKI_LOGIN)) ;
DBG("out") ;
}

function submitEditPage()
{
DBG("in") ;

  var page_title = 'New Page' ;

  OpenUrl(EDIT_URL.replace(TITLE_URL_REGEX , page_title) , function()
  {
    QuitOnErr(Page.evaluate(function(content_input_id , edit_form_id , content_text)
    {
      var content_input    = document.getElementById(content_input_id) ;
      var edit_form        = content_input.form ;
      var existing_content = content_input.textContent ;

console.log("submitEditPage() content_input.textContent=" + existing_content) ;

      if (!content_input || !edit_form) return "invalid edit page" ;
    } , CONTENT_INPUT_ID , EDIT_FORM_ID)) ;
  }) ;

DBG("out") ;
}


/* main loop */

function DefineStep(step_function , should_wait_for_pageload)
{
  var step_data                = {} ;
  step_data[STEP_FUNCTION_KEY] = step_function ;
  step_data[PAGELOAD_WAIT_KEY] = should_wait_for_pageload ;
  Steps.push(step_data) ;
}

function MainLoop()
{
  if (IsLoading) return ;

  var step_data = Steps[StepN] ;
  var step      = step_data && step_data[STEP_FUNCTION_KEY] ;
  var wait      = step_data && step_data[PAGELOAD_WAIT_KEY] ;

  if (ShouldQuit) LOG("forced quit") ;
  if (typeof step === 'function') ++StepN ; else ShouldQuit = true ;

  if (ShouldQuit) { clearInterval(MainIvl) ; LOG("done") ; phantom.exit() ; }
  else            { LOG("Step " + StepN + ": " + step.name) ; step() ;      }
}


/* helpers */

function OpenUrl(url , callback)
{
DBG("url=" + url) ;

  Page.open(url , function(status)
  {
    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 ;  ARGS.apply("Page.onLoadStarted" , arguments) ;              } ;

Page.onLoadFinished = function() { IsLoading = false ; ARGS.apply("Page.onLoadFinished " + Page.url , arguments) ; } ;

Page.onUrlChanged   = function() {                     ARGS.apply("Page.onUrlChanged"  , arguments) ;              } ;


/* logging */

Page.onConsoleMessage = function(msg) { LOG(msg) ; } ;

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])) ; }


/* main entry */

DefineStep(prepare        , false) ;
DefineStep(login          , true ) ;
DefineStep(verifyLogin    , false) ;
DefineStep(submitEditPage , true ) ;

MainIvl = setInterval(MainLoop , 250) ;