Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

[Free Nodelet Hack Feature Request] "POST 2 GET"

by blazar (Canon)
on Nov 25, 2008 at 09:49 UTC ( #725803=monkdiscuss: print w/ replies, xml ) Need Help??

I personally believe I've discussed this before, but I can't find any reference ATM, however I'm sure that there were some knowledgeable monks agreeing with me: it was not in the context of the Free Nodelet, though, but as a general consideration about this site.

The problem: I browse PM from http://perlmonks.org/ thus this very node for me is identified by the URL http://perlmonks.org/?node_id=725803. Now, if I do any operation (bringing back to the very same page) requiring a POST, up to a seemingly harmless /msg, then the "new" URL of the page will be http://perlmonks.org/?. Granted, generally this is not a problem since you either close the page itself, or follow links, and so on. But I often leave a tab open to "check it later..." And guess what? Crashes happen! Rarely enough, fortunately, but they do. FF does a good job of recovering after them, but on those tabs I'm basically redirected to the Monastery Gates. Or... it's even happened to me to bookmark some of them - and then I was bookmarking the Monastery Gates again, rather than what I really wanted.

Thus I'm shamelessly asking the JS-gurus hanging around here if they could concoct up a tiny script that would check the URL for the current page and if it does not have either a node parameter or a node_id one, then find the latter (I seem to have understood that pages are regular enough to allow one to do so reliably) and reload the page redirecting there.

I am aware that this would impose some overhead on the server; what's worst, in fact, is that the whole page up to the Free Nodlet must be loaded for the script contained in it to take effect. (In which case, as a tiny optimization, one would probably include it there as the first one.) Thus, as an alternative one may consider using Greasemonkey instead: I believe (rightly so?!?) that the script itself may stay pretty much the same with the difference that it could take effect as early as possible (rightly so, too?!?) and I didn't mention it in the first place because

  • every time I did so, someone mentioned that "now there's the Free Nodelet;"
  • it is much easier than one would want to enable the FN and JS code there than installing GM.

Update: [Tue Nov 25 11:37:33 2008] I only did a pair of tests with ccn's code but indeed they worked like a charm: thank you very much, fellow monk!

--
If you can't understand the incipit, then please check the IPB Campaign.

Comment on [Free Nodelet Hack Feature Request] "POST 2 GET"
Select or Download Code
Re: [Free Nodelet Hack Feature Request] "POST 2 GET"
by ccn (Vicar) on Nov 25, 2008 at 10:29 UTC

    Update: [Tue, 25 Nov 2008 17:40:25 GMT] Disable the feature for strangedocs, and use macros as jdporter said.

    <script type="text/javascript"> <!-- function blazar() { if ('strangedoc' == '`root_type_title`') return; var frms = document.forms; for (var i=0; i < frms.length; i++) frms[i].action = frms[i].action + 'node_id=`id`'; } setTimeout('blazar()', 500); // --> </script>
    <script type="text/javascript"> function blazar() { if(/(node_id=\d+|node=`[^;&`]+)/.test(document.location.search)) { var frms = document.forms; for (var i=0; i < frms.length; i++) frms`[i`].action = frms`[i`].action + RegExp.$1; } } setTimeout('blazar()', 500); </script>

    I personally believe *wink* that POST to GET substitution is not good. Just because a browser would not ask to resubmit posted data and the user will unintentionally repost own messages to CB, etc.

    So I suggest an ugly hack to add GET parameters (node_id or node) to forms' actions

      See Help for Free Nodelet Settings for info on the value macros available.

      function blazar() { var frms = document.forms; for (var i=0; i < frms.length; i++) frms[i].action = frms[i].action + 'node_id=`id`'; } setTimeout('blazar()', 500);
      Between the mind which plans and the hands which build, there must be a mediator... and this mediator must be the heart.
Re: [Free Nodelet Hack Feature Request] "POST 2 GET"
by shmem (Canon) on Nov 25, 2008 at 11:09 UTC
    Thus I'm shamelessly asking the JS-gurus hanging around here if they could concoct up a tiny script that would check the URL for the current page and if it does not have either a node parameter or a node_id one, then find the latter (I seem to have understood that pages are regular enough to allow one to do so reliably) and reload the page redirecting there.

    I'm not a guru, but this snippet should do the job. It checks the current url, and if it just ends in /?, it gets the current pages node ID via XPath and redirects there.
    NO WARRANTIES. Although I've tested it, please keep a Free Nodelet Settings tab open without this hack in it, to emergency-post that in case something goes wrong.

    var uri = new String(document.location); if (uri.match(/\/\?$/)) { var id = document.evaluate("/html/body/table`[@id='titlebar-bottom'] +/tbody/tr/td`[1]/span/a`[1]", document, null, XPathResult.ORDERED_NOD +E_SNAPSHOT_TYPE, null).snapshotItem(0).innerHTML; id = parseInt(id); if (id != 11911) { uri = uri.replace(/\?/,"?node_id=" + id); document.location = uri; } }

    There might be other cases than post previews for which redirection isn't wanted; adding those is left as an exercise to the reader ;-)

    update: scratch that, ++ccn's solution looks better... well, TIMTOWTDI ;)

Re: [Free Nodelet Hack Feature Request] "POST 2 GET"
by ambrus (Abbot) on Nov 25, 2008 at 16:45 UTC

    Wouldn't this break Super Search and all the other pages with content depending on the parameters?

      I personally believe that as of some previous discussions hinted to in the root node, in fact a "general consensus" emerged that it should be something handled at a somewhat more primitive level. In the absence of this, a JS based solution and appearantly just as reliable as those shown thus far, seems like a good first order approximation: indeed I'm browsing the Monastery with pleasure and no annoying side effects today. I'm still keeping my eyes open for possible gotchas.

      --
      If you can't understand the incipit, then please check the IPB Campaign.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: monkdiscuss [id://725803]
Approved by ccn
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (13)
As of 2014-12-18 13:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (52 votes), past polls