A better user script for HKEJ.com
Last time I have shown a very simple/stupid user script to auto login hkej.com ( 信報財經新聞網站 )
In fact, what it does is not auto log in, but check if you are checked out and try to log-in once. It does not safe you ( me ) from cookie expires. Although it does re-log and so user can go back and click on the link to escape.
I find no way to manipulate cookie with GreaseMonkey directly. This is not too bad in terms of security. I don’t have to steal cookies. I just want to revive them.
1) The simplist trick is to reload the page:
window.setTimeout(window.location.reload,29min_later);
It works. But what if the user left some valuable words in forms ?
All such data will be vanished.
2) Do some minimal ajax load
I cannot sure why, but it does not work. It seems the ajax load within Greasemonkey is bounded by some shields.
3) Load some data into an iframe, and reload the iframe from time to time.
It works. Rather than expected, I can hide those iframe without anyharm.
So I rewrite the code a little bit. This time, I have also taken reference from a famous user script to handle username and password.
Wondering if I shall submit it to userscripts.org ….
Would do so if anyone interested.
// ==UserScript== // @name Hacks on HKEJ // @namespace info.bencrox // @description Make life easier with hkej.com // @include http://*.hkej.com/* // @exclude http://*.hkej.com/*/adv/* // @exclude http://*.hkej.com/*/login.jsp // ==/UserScript== // ! Private Credentials, BEWARE ! LOGIN_EMAIL = GM_getValue('ejregem',''); LOGIN_PASS = GM_getValue('ejregpw',''); // Note : lots FIXME // by : lxb [at] hkday [dot] net // ver : 0.1.013 [ 2008 Jul 25 20:00:04 ] // ! Change Log ! // 0.1.000 [ 2008 Jul 25 18:01:12 ] : base GM + jQ ref htt://www.joanpiedra.com/jquery/greasemonkey // 0.1.001 [ 2008 Jul 25 18:13:02 ] : add checkLogin() , use Google hosted jQuery // 0.1.002 [ 2008 Jul 25 18:13:02 ] : checkLogin() add fail handling // 0.1.003 [ 2008 Jul 27 19:02:13 ] : add excludes // 0.1.004 [ 2008 Jul 27 22:04:20] : test window.location.reload and GM Menu // 0.1.005 [ 2008 Jul 27 22:07:56] : test with partial ajax reload, failed // 0.1.006 [ 2008 Jul 27 22:13:28] : test with iframes, worked // 0.1.007 [ 2008 Jul 28 17:35:44] : reload iframes in hidden div by setTimeout // 0.1.008 [ 2008 Jul 29 21:50:13] : test with GM_setValue / getValue // 0.1.009 [ 2008 Jul 30 16:02:32] : fixed GM_setValue by not using jQuery // 0.1.010 [ 2008 Jul 30 16:04:58] : allow manual reset un/pw , blank pw , ref : http://userscripts.org/scripts/review/16341 // 0.1.011 [ 2008 Jul 30 16:56:16]: fix re-bake loop by adding excludes and choosing better dummy pages // 0.1.012 [ 2008 Jul 30 17:15:21]: test with extra styling / manual items // 0.1.013 [ 2008 Jul 30 20:00:04]: clean up test codes // - Change Log - // Add jQuery var GM_JQ = document.createElement('script'); GM_JQ.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js'; GM_JQ.type = 'text/javascript'; document.getElementsByTagName('head')[0].appendChild(GM_JQ); // Check if jQuery's loaded function GM_wait() { if(typeof unsafeWindow.jQuery == 'undefined') { window.setTimeout(GM_wait,100); } else { $ = unsafeWindow.jQuery; letsJQuery(); } } GM_wait(); //renewCookie by fetch the smallest pieces. //FIXME: they are not quite small... seems idiot method... function renewCookie() { $('#webifm').attr('src','http://web.hkej.com/template/registration/jsp/login.jsp'); $('#wwwifm').attr('src','http://www.hkej.com/template/registration/jsp/login.jsp'); } //call renew cookies every 25mins function cookieTimeout() { renewCookie(); window.setTimeout(cookieTimeout,1500000); } //provide some way to reset Creds stored in firefox //FIXME: tell me if I can encrypt things ... -_- function resetCredentials(){ if ((LOGIN_EMAIL = prompt('Registered Email:')) != null){ GM_setValue('ejregem',LOGIN_EMAIL); } else return; if ((LOGIN_PASS = prompt('Password (leave blank to prompt every time):')) != null){ GM_setValue('ejregpw',LOGIN_PASS); } } // Provide a button to reset Creds if login failed function checkLogOK(){ // FIXME: Yes, this must lead to faults afterwards ts = document.getElementsByTagName('b') if(ts.len){ // FIXME: Cannot use jQuery (GM_setValue within unsafeWindow ) here, resetp = document.createElement('button'); resetp.innerHTML='Reset'; resetp.addEventListener('click',resetCredentials,true); ts[0].appendChild(resetp); } } // Extract the Log in Operation for manual use function AutoLog() { if (LOGIN_EMAIL == '') resetCredentials; if (LOGIN_PASS == '') LOGIN_PASS = prompt('Password :'); // FIXME : this must fail afterward, yes, yes... $('#wrapper table:first td:eq(1)') .load('/template/registration/jsp/_login_prg.jsp #regWrap b', {login:LOGIN_EMAIL,password:LOGIN_PASS},checkLogOK); } // Check login function checkLogin() { // FIXME : it may not work if hkej update itself var login = $('img#Reg_Log').attr('src').indexOf('logout') + 1; if (!login) AutoLog(); cookieTimeout(); } // Hide the cookie ovens function Addiframe(){ $('<div id= "hid"></div>').appendTo('body'); $('<iframe name="webifm" id="webifm"></iframe>').appendTo('#hid'); $('<iframe name="wwwifm" id="wwwifm"></iframe>').appendTo('#hid'); $('#hid').hide(); } // All your GM code must be inside this function function letsJQuery() { Addiframe(); checkLogin(); } GM_registerMenuCommand('Login', AutoLog); GM_registerMenuCommand('Reset ID',resetCredentials);
Have fun!
