User talk:Bazzargh/citemark

From Wikipedia, the free encyclopedia

Contents

[edit] Todo

At the moment, only the first text node under an h1, h2 node gets captured. For URLs like [1], this isn't enough, because the author has nested bold tags inside the h2 that held the subtitle. This is quite unusual for h1, h2, but sometimes the title will just be a styled paragraph. I should update the code to extract all the text, not just the first node. This improves h(t) with xpath:

pre
function h(t){return e(t,document.evaluate('//'+t+'[1]', document, null, XPathResult.STRING_TYPE, null).stringValue);}

--Bazzargh (talk) 01:35, 8 February 2008 (UTC)

The script below is what I'm currently using. The xpath above isn't used - I found that this stops the bookmarklet working on sites with javascript disabled in FF3 (using NoScript). I've removed some duplicate checks, and added the ability to grab the selection too - this will appear as the 'quote' but of course you could use this just to grab something off the page that also included your metadata. If what you grab is a date, it will be reformated to yyyy-mm-dd format. I've not tried to do selection in a cross-browser way, since this bookmarklet is too large for IE anyway.

javascript:
function h(t){var z=document.getElementsByTagName(t)[0]; return z?e(t,z.childNodes[0].nodeValue):'';}
function e(a,b){var d=Date.parse(b);return a&&b?('|'+a+'='+(d?f(d):b)+'\n'):''}
function z(x){return x<10?'0'+x:x}
function f(x){ var d=new Date(x);return d.getFullYear()+'-'+z(d.getMonth()+1)+'-'+z(d.getDate()) }
var s=e('url',window.location)
+e('accessdate',Date())
+e('title',document.title)
+e('date',document.lastModified)
+e('quote',window.getSelection()+'')
+h('h1')+h('h2');
var m=document.getElementsByTagName('meta');
for(var i=0;i<m.length;i++){s+=e(m[i].getAttribute('name'),m[i].getAttribute('content'))} 
alert('{{cite web'+s+'}}')

I experimented with popping up a separate window, but then I have to add in lots of html escaping code; building a new page with the DOM instead of document.write (to avoid escaping) seemed unreliable for some reason. Suggestions welcome, I don't write JS much. --Bazzargh (talk) 12:42, 8 February 2008 (UTC)

[edit] Known Bugs

  • There are some pages where the bookmark doesn't pop up an alert (FF3b2). This is an example: [2] —Preceding unsigned comment added by Bazzargh (talkcontribs) 17:03, 8 February 2008 (UTC)
fixed. --Bazzargh (talk) 17:12, 8 February 2008 (UTC)

[edit] IE 7

This does not work with Internet Explorer 7. When one tries to cut and paste from Wikipedia to the URL field in the create bookmark field, all that is copied is:

javascript:function%20e(a,b){var%20d=Date.parse(b);return%20a&&b?('|'+a+'='+(d?f(d):b)+'\n'):''}function%20z(x){return%20x<10?'0'+x:x}function%20f(x){var%20d=new%20Date(x);return%20d.getFullYear()+'-'+z(d.

--Gerry Ashton (talk) 18:02, 8 February 2008 (UTC)

Thanks for the feedback. I have tested that in IE7 and it did work - but I'm not in front of a windows machine again until Monday. I'll look into it. --Bazzargh (talk) 21:46, 8 February 2008 (UTC)
BTW just to explain how I tested this - I edit the scripts in emacs, copy them into FF3b2 as URLs (this URL-encodes them on input). I then reopen the bookmark in FF and copy the link to a favourite in IE7, and test them. I'd not actually tried copying the URL from wikipedia in IE7. Oddly enough you've managed to copy 205 chars, almost exactly half the limit in IE (508)[3], I'm wondering if there's some double-byte char nonsense going on here. --Bazzargh (talk) 22:18, 8 February 2008 (UTC)
I don't plan to pursue this further, since I only wanted to see what data it would retrieve; I generally avoid {{tl:cite web}} if I can. Also, I don't think it would help me all that much, since I usually find I have to look at one site for the information, another site for the date, and still another site for the proper name of the author or publisher. --Gerry Ashton (talk) 23:00, 8 February 2008 (UTC)
FWIW, I retested this on IE7 (7.0.5730.13) XP Pro (5.1.2600) and it works fine for me; triple-clicking on the URL selected it all, it copied fine, the popup worked, and the copy and paste worked (Ctrl-A Ctrl-C; IE7 is another browser where it won't highlight the selection in an alert). I'll leave it up there, perhaps someone else will find it useful; its also easier to adapt this version to write citations without a template, if someone wants that. --Bazzargh (talk) 10:22, 11 February 2008 (UTC)

[edit] Opera

FYI, the Firefox version works just fine under Opera as well. RossPatterson (talk) 00:17, 9 April 2008 (UTC)

Cool, thanks! Bazzargh (talk) 09:17, 9 April 2008 (UTC)