I'm looking to write the following module, any help, guidance, and advice would be greatly appreciated.

  • User is logged into site.
  • Site has two content types: page & note.
  • Logged in user can create content type note only.
  • User is viewing a node: /node/5.
  • User selects some text in the body.
  • Link/icon is shown above selected text.
  • User clicks clicks and is taken to /node/add/note
  • Some fields are pre-populated on Node edit page, including:
    • title, field_source_quote, field_entity_reference_nid
  • User adds his "note" to field_user_note and saves note

From my basic understanding of JSON, I should be able to create a JSON of the user selected text with something like:

var selectedText = "";
selectedText = window.getSelection().toString();

var noteData={
    "sourcePath": "/node/being/viewed",
    "selectedText": selectedText

How do I get noteData to a module? Better yet, how do I get nodeData to the node/add/note page and pre-populate the correct fields?

Any help would be greatly appreciated.

======= Updates Below =============

Passing data through local storage (Andrew's Approach)

I can confirm this approach is viable. There are a number of issues, however that this presents:

  1. I'm sure this is not the most "drupal-ized" way to perform this.
  2. If the node admin pages use a different theme, the js to retrieve and apply the data must be within the other them.
  3. Need to have a persistent marker placed for the user ui (i.e. highlighting the text they have added a "note" to).

I've come across a library that might be helpful for #3: Rangy (https://github.com/timdown/rangy).

1 Answer 1


Here is a little working piece of code for you. You may extend it according to your needs

document.onselectionchange = function() {
    var selectedText = "";
    $('div').mouseup(function() {
      //Save selected text in localStorage.
      selectedText = window.getSelection().toString();
      if (selectedText.length > 10) {
        var noteData={
          "sourcePath": window.location.href,
          "selectedText": selectedText
        localStorage.setItem('noteData', JSON.stringify(noteData));
        window.location.href = '/node/add/page';

  // Get saved data from localStorage and insert in the form.
  var savedNoteData = JSON.parse(localStorage.getItem('noteData'));
  if (savedNoteData && savedNoteData.selectedText) {
    $('.form-item-title input').val(savedNoteData.selectedText);
    localStorage.setItem('noteData', '');
  • Interesting, so you're saying that I can pass/retrieve noteData via localStorage... I never thought of that - I will give that a shot and report back.
    – ymdahi
    Jul 10, 2015 at 14:12
  • I've updated the post to reflect your idea. I'll give another couple of weeks to see if any one else has any options.
    – ymdahi
    Jul 11, 2015 at 3:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.