// ==UserScript==
// @name           WP-Bloglines
// @namespace      http://rephrase.net/box/user-js/
// @description    Replaces the Bloglines "Clip/Blog This" link with a link to a WordPress post bookmarklet.
// @include        *bloglines.com/myblogs_display*
// ==/UserScript==

/* 
Changes
-------
2006-07-29: update to fix changes after recent BlogLines change. Don't even try to getSelection().


Instructions
------------
Modify the wp_address variable below to the location of your WordPress installation.

*/

// Address of WordPress install (i.e. directory containing /wp-admin and /wp-content)
var wp_address = 'http://192.168.0.167/multiply'; 

    
// Redefine the saveItem() function BlogLines uses
unsafeWindow.saveItem = function(blogId, blogItemId) {
    
    var node = document.getElementById('article-' + blogId + '-' + blogItemId);
    if (node != null) {
        /*
        Get the next node (a H3), but skip intervening whitespace nodes.
        */
        while (node = node.nextSibling) {
            if ( node.nodeType == 8 || (node.nodeType == 3 && !(/[^\t\n\r ]/.test(node.data))) ) {
                continue;
            } else {
                break;
            }
        }
        
        /*
        We want to get the selected text and automatically put it in the bookmarklet,
        but changing focus to the link (i.e. on click) deselects said text and leaves
        nothing to send. I've tried half a dozen workarounds and can't get it to work.
        I give up.

        var itemText = window.getSelection().toString();
        var itemText = node.ownerDocument.getSelection().toString();
        */
        var itemText = "";

        var usefulItem = node.innerHTML;

        var itemLink = usefulItem.match(/href="(.*?)"/);
        itemLink = itemLink[1];

        var itemTitle = usefulItem.match(/>(.*?)<\/a>/);
        itemTitle = itemTitle[1];
    }
        
    var bmAddress = wp_address.replace(/\/$/, '') + '/wp-admin/bookmarklet.php?';

    if (typeof itemText != 'undefined') bmAddress += 'text=' + encodeURIComponent(itemText);
    if (typeof itemLink != 'undefined') bmAddress += '&popupurl=' + encodeURIComponent(itemLink);
    if (typeof itemTitle != 'undefined') bmAddress += '&popuptitle=' + encodeURIComponent(itemTitle);

    var windowTitle = 'WordPress Bookmarklet';
    var windowSpecs = 'scrollbars=yes, width=600, height=460, left=100, top=150, status=yes';
    void( window.open(bmAddress, windowTitle, windowSpecs) );
}