Drupal & shell bookmarklet for updating modules 2 ->just Recommended

Note *A new post is available here: Drupal & shell bookmarklet for updating modules 5 (updated – mulitple dbs, backup only option)

I just posted about a bookmarklet I wrote to help with processing drupal module updates.
After looking at the update (admin/reports/updates) page today. I noticed that there are occasionally more than one download link for each module. Some marked ‘Also available:’ as well as ‘Development Version’. So the new challenge is finding the recommended one.

After a bit of thinking I worked out a new bookmarklet. This time capitalizing on jQuery to grab the desired links:
Install all mods jQuery

It’s quite a bit shorter than the last version. 1164 characters vs. 2433 characters.
I would suspect that if I knew more jQuery I could replace quite a bit more as well; though most of the savings are related to the lack of selection highlight or direct entry support. The other bookmarks (wget in particular) will still be quite handy for installing from the Drupal site.

As with the last post, please keep in mind that this bookmark is just for my convenience. Which means I haven’t done any cross-browser testing. I only use them in Firefox on Mac at this point. Feel free to make use of the code and update it as you see fit. Personally I’ve created versions without all the pop up prompts for my primary sites. It may be worth pointing out that wordpress has been muddling with the code a bit too. So if the bookmarklet doesn’t work that may be why. It works well for me so I wanted to share.

Here’s an example of the output for all of my pending updates:

wget http://ftp.drupal.org/files/projects/counter-6.x-2.3.tar.gz
gzip -dc counter-6.x-2.3.tar.gz | tar -x
rm counter-6.x-2.3.tar.gz
wget http://ftp.drupal.org/files/projects/diff-6.x-2.1-alpha3.tar.gz
gzip -dc diff-6.x-2.1-alpha3.tar.gz | tar -x
rm diff-6.x-2.1-alpha3.tar.gz
wget http://ftp.drupal.org/files/projects/pathauto-6.x-2.x-dev.tar.gz
gzip -dc pathauto-6.x-2.x-dev.tar.gz | tar -x
rm pathauto-6.x-2.x-dev.tar.gz
wget http://ftp.drupal.org/files/projects/views-6.x-3.0-alpha1.tar.gz
gzip -dc views-6.x-3.0-alpha1.tar.gz | tar -x
rm views-6.x-3.0-alpha1.tar.gz
wget http://ftp.drupal.org/files/projects/webform-6.x-2.9.tar.gz
gzip -dc webform-6.x-2.9.tar.gz | tar -x
rm webform-6.x-2.9.tar.gz

Whoa that’s a lot of updates… Dropped that in Terminal and I’m all done in 20 seconds!
Here’s the code:

javascript:
(function(){
var%20connect=confirm('Would%20you%20like%20to%20include%20the%20connection%20information?');
var%20shellLocation=prompt('What%20is%20the%20location%20of%20your%20install%20directory?','/var/www/html/YOUR_SITE_HERE/sites/all/modules/');
var%20shellText=%22%22;
if(connect){
shellText=%22ssh%20vpfa-prod;
%22;
}
if(shellLocation!=null){
shellText=shellText+%22
cd%20%22+shellLocation;
}
with($('table.version-recommended td li.update-download a').each(function() {

q=this.href.toString().split('/').pop();
if(q!==%22%22&&this.href!=%22%22){
shellText=shellText+%22
wget%20%22+unescape(this.href);
shellText=shellText+%22
gzip%20-dc%20%22+escape(q)+%22%20|%20tar%20-x%22;
shellText=shellText+%22
rm%20%22+escape(q);
}

}));

if(connect){
shellText=shellText+%22
wait%22;
shellText=shellText+%22
ls%20-al%22;
}
if(shellText!==%22%22){
if(window.clipboardData){
window.clipboardData.setData('Text',shellText);
}
else{
wnd=open('','Shell_Command','width=500,height=400,top=0,left=0,scrollbars,resizable');
with(wnd.document){
writeln('
<pre>');
        writeln(shellText);
        writeln('</pre>
');
}
wnd.document.close();
}
}
/*if%20shellText!==%22%22*/

}
)()

Here are all the steps I went through to get from there to here, notes included;

First off I want to identify a link and then check all the parents for the work “recommended”.
For example the webform module has two links right now:

//I discovered through the process of elimination that the link number for the download link is 164:
javascript:alert(document.links[164].innerHTML) //= Download
javascript:alert(document.links[164].href) // = http://ftp.drupal.org/files/projects/webform-6.x-2.9.tar.gz
javascript:alert(document.links[164].parentNode.parentNode.parentNode.parentNode.innerHTML) /* =

Recommended version: <a href="http://drupal.org/node/624964">6.x-2.9</a> <span class="version-date">(2009-Nov-05)</span>
<ul class="links">
	<li class="update-download first"><a href="http://ftp.drupal.org/files/projects/webform-6.x-2.9.tar.gz">Download</a></li>
	<li class="update-release-notes last"><a href="http://drupal.org/node/624964">Release notes</a></li>
</ul>
javascript:alert(document.links[164].parentNode.parentNode.parentNode.parentNode.childNodes[0].innerHTML) /* =Recommended Version: */
//Allright.

//Here's the next link for the alpha version:
javascript:alert(document.links[167].innerHTML) // Download
//and
javascript:alert(document.links[167].parentNode.parentNode.parentNode.parentNode.childNodes[0].innerHTML) /*=Also available:*/
Great!

OK so I’ve isolated the current position of the descriptor label relative to the download link. Unfortunately it relies on the structure of update page NOT changing… Oh well it works for now!
Here’s the selected html code I’m referencing:

Update availablewarning
Webform 6.x-2.8
Also available: 6.x-3.x-dev (2009-Nov-06)
Includes: Webform
<table class="version version-recommended version-recommended-strong"><tr><td class="version-title">Recommended version:</td>
<td class="version-details"><a href="http://drupal.org/node/624964">6.x-2.9</a> <span class="version-date">(2009-Nov-05)</span></td>
<td class="version-links"><ul class="links"><li class="update-download first"><a href="http://ftp.drupal.org/files/projects/webform-6.x-2.9.tar.gz">Download</a></li>
<li class="update-release-notes last"><a href="http://drupal.org/node/624964">Release notes</a></li>
</ul></td></tr></table>
<table class="version version-also-available"><tr><td class="version-title">Also available:</td>
<td class="version-details"><a href="http://drupal.org/node/459558">6.x-3.x-dev</a> <span class="version-date">(2009-Nov-06)</span></td>

<td class="version-links"><ul class="links"><li class="update-download first"><a href="http://ftp.drupal.org/files/projects/webform-6.x-3.x-dev.tar.gz">Download</a></li>
<li class="update-release-notes last"><a href="http://drupal.org/node/459558">Release notes</a></li>
</ul></td></tr></table>
</div>
<div class="info">
<div class="includes">Includes: <em>Webform</em></div>
</div>
</td> </tr>
</table>

So with jquery, you could loop through each tr.warning and check the table.version-recommended td.version-title innerHTML or table.version-recommended-strong td.version-title innerHTML and ignore the table.version-also-available.
Once that’s found grab the link in the td.update-download a.href
That might be more fun and less likely to break later.

This site uses jQuery so It’s available to us.
The reason I didn’t start with it is that I had intended on capture the link properties as it was clicked and hit a dead end in both jQuery and javascript but by then I was well invested in the js.

javascript:alert($('table.version-recommended td.version-title')[0].innerHTML);// = Recommended version

OK so that worked…
Hmm how about

javascript:if($('table.version-recommended td.version-title')[0].innerHTML == "Recommended version:"){alert($('table.version-recommended td li.update-download a')[0].href)} //= http://ftp.drupal.org/files/projects/webform-6.x-3.x-dev.tar.gz

This might come in handy too:

javascript:alert($('tr.warning .project a')[0].innerHTML) //diff 
//this returns all the recommended downloads:
javascript:(function(){  $('table.version-recommended td li.update-download a').each(function() {alert(this.href)}); })()

So this might be the cleaner way to return all the recommended downloads:

javascript:
(function(){
shellText="";
with($('table.version-recommended td li.update-download a').each(function() {

q=this.href.toString().split('/').pop();
if(q!==""&amp;&amp;this.href!=""){
shellText=shellText+"n
wget%20"+unescape(this.href)+";";
shellText=shellText+"n
gzip%20-dc%20"+escape(q)+"%20|%20tar%20-x;";
shellText=shellText+"n
rm%20"+escape(q)+";";
}
}));
alert(shellText);
}
)()

and here is the completed code:

javascript:
(function(){
var%20connect=confirm('Would%20you%20like%20to%20include%20the%20connection%20information?');
var%20shellLocation=prompt('What%20is%20the%20location%20of%20your%20install%20directory?','/var/www/html/YOUR_SITE_HERE/sites/all/modules/');
var%20shellText=%22%22;
if(connect){
shellText=%22ssh%20vpfa-prod;
%22;
}
if(shellLocation!=null){
shellText=shellText+%22
cd%20%22+shellLocation;
}
with($('table.version-recommended td li.update-download a').each(function() {

q=this.href.toString().split('/').pop();
if(q!==%22%22&amp;&amp;this.href!=%22%22){
shellText=shellText+%22
wget%20%22+unescape(this.href);
shellText=shellText+%22
gzip%20-dc%20%22+escape(q)+%22%20|%20tar%20-x%22;
shellText=shellText+%22
rm%20%22+escape(q);
}

}));

if(connect){
shellText=shellText+%22
wait%22;
shellText=shellText+%22
ls%20-al%22;
}
if(shellText!==%22%22){
if(window.clipboardData){
window.clipboardData.setData('Text',shellText);
}
else{
wnd=open('','Shell_Command','width=500,height=400,top=0,left=0,scrollbars,resizable');
with(wnd.document){
writeln('
<pre>');
        writeln(shellText);
        writeln('</pre>
');
}
wnd.document.close();
}
}
/*if%20shellText!==%22%22*/

}
)()
Comments
Leave a Comment