Changes between Version 1 and Version 2 of LoggedInScript


Ignore:
Timestamp:
Dec 22, 2008, 11:18:08 PM (6 years ago)
Author:
dwclifton
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • LoggedInScript

    v1 v2  
    1 = Make logged in as [user] link to homepage =
     1= !LoggedInScript =
    22
    33== Description ==
    44
    5 Will fill this in
     5Ever wonder why Trac doesn't link to the user's home page from the first
     6`metanav` "logged in as" item? I sure did. I cannot recall having an account
     7on a site of some kind that didn't have this feature. So of course I set
     8out to create a hack to add it to Trac.
     9
     10Of course you could do this sort of thing with [http://genshi.edgewall.org/ Genshi],
     11something I'm still trying to wrap my head around and, frankly, I'm not a big fan of these
     12XML/XSLT-based [http://genshi.edgewall.org/wiki/Documentation/xml-templates.html templating languages].
     13
     14Since Trac loads [http://jquery.com/ jQuery], a !JavaScript library I happen to
     15be a ''huge'' fan of, I thought, why not let the browser do some of the work for
     16a change?
     17
     18You might think, since you are parsing the DOM and checking for certain
     19attributes and values, won't your code break if Trac changes something in
     20the future (remember the bad old days of "screen scraping")? While this
     21concern certainly has some merit, if you think about it it's essentially
     22doing the same thing as a Python/Genshi approach by manipulating the page
     23on the fly.
     24
     25At any rate, this hack has no repository or download link or any of that.
     26Just a simple code snippet. Ironically, this goes in your site.html
     27template and it does use Genshi template markup.
     28
     29== Code ==
     30
     31{{{
     32<html xmlns="http://www.w3.org/1999/xhtml"
     33      xmlns:py="http://genshi.edgewall.org/" py:strip="">
     34 <head py:match="head" py:attrs="select('@*')">
     35  ${select('*')}
     36  <script type="text/javascript">
     37   jQuery(document).ready(function($) {
     38     $('#metanav li.first:contains("logged in as")').each(function() {
     39       var path = $('#mainnav li.first a').attr('href').split('/')[1];
     40       var user = $(this).text().split(' ').pop();
     41       var link = '<a href="/' + path + '/wiki/' + user + '">' + user + '</a>';
     42       $(this).html('logged in as ' + link);
     43       return false;
     44     });
     45   });
     46  </script>
     47 </head>
     48</html>
     49}}}
    650
    751== Bugs/Feature Requests ==
     
    1357[http://trac-hacks.org/newticket?component=LoggedInScript&owner=dwclifton new ticket].
    1458
    15 == Download ==
    16 
    17 Download the zipped source from [download:loggedinscript here].
    18 
    19 == Source ==
    20 
    21 You can check out LoggedInScript from [http://trac-hacks.org/svn/loggedinscript here] using Subversion, or [source:loggedinscript browse the source] with Trac.
    22 
    23 == Example ==
    24 
    25 &lt;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;
    26       xmlns:py=&#34;http://genshi.edgewall.org/&#34; py:strip=&#34;&#34;&gt;
    27  &lt;head py:match=&#34;head&#34; py:attrs=&#34;select('@*')&#34;&gt;
    28   ${select('*')}
    29   &lt;script type=&#34;text/javascript&#34;&gt;
    30    jQuery(document).ready(function($) {
    31      $('#metanav li.first:contains(&#34;logged in as&#34;)').each(function() {
    32        var path = $('#mainnav li.first a').attr('href').split('/')[1];
    33        var user = $(this).text().split(' ').pop();
    34        var link = '&lt;a href=&#34;/' + path + '/wiki/' + user + '&#34;&gt;' + user + '&lt;/a&gt;';
    35        $(this).html('logged in as ' + link);
    36        return false;
    37      });
    38    });
    39   &lt;/script&gt;
    40  &lt;/head&gt;
    41 &lt;/html&gt;
    42 
    4359== Recent Changes ==
    4460
     
    4763== Author/Contributors ==
    4864
    49 '''Author:''' [wiki:dwclifton] [[BR]]
    50 '''Contributors:'''
     65 * '''Author:''' [wiki:dwclifton]
     66 * '''Contributors:''' [http://ejohn.org/ John Resig]