bookmarklet script for userlink

jwilde - August 20, 2006 - 14:12
Project:userlink
Version:HEAD
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active
Description

Hi,

Thanks for the great module. I'm trying to add a bookmarklet, jscript, javascript:location.href='http://www.mysite.com/node/add/userlink?edit[url]='+encodeURIComponent(location.href)+'&edit[title]='+encodeURIComponent(document.title)

but can't get it to work. Do you have any ideas?

Thank,

Jim

#1

jonathan_hunt - September 13, 2006 - 03:35

By default, I don't think you can pass parameters to Drupal via URLs unless Drupal expects that.

I have this bookmarklet working for me.

I made the following changes:
in the javascript
- replaced & with &
- replaced edit[url] with url and edit[title] to title
in userlink.module
- in userlink_form(), I added the following at the start of the function

if ($_GET['url'] && $_GET['title']) {
    $node->url = $_GET['url'];
    $node->title = $_GET['title'];
  }
 

HTH.

#2

jonathan_hunt - September 13, 2006 - 03:36

I meant to say replaced & with &

#3

marcp - September 13, 2006 - 14:18

Jonathan - could you please post the javascript code in a code block for me? Also, I've got a "post to del.icio.us" button on my toolbar -- I can't remember how it got there, but it seems like del.icio.us gave me a quick and easy way to bookmark the javascript code -- do you know how we can incorporate that into userlink?

Thanks,

Marc

#4

rwfaught - October 18, 2006 - 08:00

Hiya folks. I was able to get a bookmarklet for Userlink working perfectly in Firefox with the following code:

javascript:q=""+(window.getSelection?window.getSelection():document.getSelection?document.getSelection():document.selection.createRange().text);location.href='http://www.myspizace.com/node/add/userlink?edit[url]='+encodeURIComponent(location.href)+'&edit[title]='+encodeURIComponent(document.title)+'&edit[body]='+q;

That will fill in the URL and Title fields, and it will also put any selected text on the page into the Description field -- with one caveat: if you open the bookmarklet as a new tab it just fills in about:blank in the URL field and leaves everything else blank.

I'm not a programmer, I just hacked my way through this from bits and pieces of bookmarklets I have so if there's an easy fix for that then let me know. I can handle having it open in the same window in the meantime.

#5

rwfaught - October 18, 2006 - 08:22

...err, the "doesn't work in tabs" thing makes complete sense. It's late (or early) and I should be in bed. I'll figure out a pop-up or some other solution to having it open in the same window later.

#6

rwfaught - October 18, 2006 - 16:27

In my sleep-deprived state this morning I also forgot to mention that I used the prepopulate module which allows fields in the create content forms to be pre-populated from the URL.

#7

mloitzl - January 15, 2007 - 11:59

Hi!

I would suggest the following solution (best of ;-):

Variant of the Bookmarklet: Drupal "Blog this" bookmarklet v2.0 (ish)

javascript:
u=document.location.href;
t=document.title.replace(/\|/g,'::');
s='<blockquote>'+window.getSelection()+'</blockquote>';
pre='<em>From <a target="_blank" href="'+escape(u)+'">'+escape(t) + escape('</a>:</em><br /> ');
void(window.open('http://www.example.com/node/add/blog?edit[title]='+escape('Link: '+t)+'&edit[url]='+escape(u)+'&edit[body]='+pre+escape(s)+escape('<a href="'+u+'" target="_blank">'+u+'</a>'),'_blank','width=710,height=500,status=yes,resizable=yes,scrollbars=yes'));

Since the form field names are already exposed in the html source, why don't keep them?

The (robust?) php code would be:

function userlink_form(&$node) {

if(isset($_GET[edit])) {
        $default_values = $_GET[edit];
        $default_url = (isset($default_values['url']) && !empty($default_values['url'])) ? $default_values['url'] : 'http://';
        $default_body = (isset($default_values['body']) && !empty($default_values['body'])) ? $default_values['body'] : '';
        $default_title = (isset($default_values['title']) && !empty($default_values['title'])) ? $default_values['title'] : '';
}
else {
        $default_url = 'http://';
        $default_body = '';
        $default_title = '';
}


  $form['url'] = array('#type' => 'textfield', '#title' => t('URL'), '#required' => TRUE, '#default_value' => $node->url ? $node->url : $default_url, '#des\
cription' => t('The URL for this link (usually begins with http://).'), '#weight' => -50);
  $form['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#required' => TRUE, '#default_value' => $node->title ? $node->title : $default_ti\
tle, '#description' => t('The title of this link.'), '#weight' => -30);
  $form['body'] = array(
    '#type' => 'textarea', '#title' => t('Description'), '#default_value' => $node->body ? $node->body : $default_body, '#rows' => 20, '#required' => FALSE\
, '#weight' => -20
  );

  return $form;
}

Maybe a little bit paranoid...

Have fun,

Martin

#8

zaphod280380 - March 20, 2007 - 16:25

javascript:q=""+(window.getSelection?window.getSelection():document.getSelection?document.getSelection():document.selection.createRange().text);location.href='http://www.myspizace.com/node/add/userlink?edit[url]='+encodeURIComponent(location.href)+'&edit[title]='+encodeURIComponent(document.title)+'&edit[body]='+q;

this code with prepopulate module work like a charm... but i'm not skilled with javascript and i need to open the link in a new focused window. How can i do this?

thanks in advance!

 
 

Drupal is a registered trademark of Dries Buytaert.