Anonymous
Domino 2.0 Rich Internet Applications with IBM Lotus Notes/Domino
You are here: Today » Bookmark web pages in a Notes database
« Domino 7: I didn't know that!
The base JavaScript object explained »

Bookmark web pages in a Notes database

A slow start for me this year. This month, I only managed to create one blog entry. This is something I wanted to build for a very long time, not only to use in this Domino 2.0 blog, but also for my Tangosite: a way of collecting useful web pages in a Notes database.

I use a bookmarklet to gather and post information from any web page to my own Domino application.

The bookmarklet

On some HTML page, I created a bookmarklet that you can right-click and select 'Add to favorites' (IE). In IE, you can add it to 'Links'. You get a security warning. Ignore it.

<a href="javascript: var o=document.createElement('script'); o.type='text/javascript'; o.src='/[WebDbPath]/bookmark.js'; document.getElementsByTagName('head')[0].appendChild(o); void null;">Bookmark this</a>

Once you got the bookmarklet, surf to any HTML page, click the 'Bookmark this' link, and the bookmark.js script comes into action, posting the metadata of the page to the Notes database. When you insert your own JavaScript in a page on another server like this, you have full access to the page and all pages of that domain. While you are using this, you have to disable popup blockers.

The bookmark.js script

This script gathers information of the page (title, h1, meta tags), creates a form on the page to create a Notes document and posts the form. The return value is displayed in a popup window. Creating a form and posting it using DOM scripting is something I already explained in Validate xhtml of a site in development with Ajax.

/* bookmark */
var Bookmark={
get:function(a,b){return(b||document).getElementById(a)},
tags:function(a,b){return(b||document).getElementsByTagName(a)},
tag:function(a,b){return this.tags(a,b)[0]},
set:function(a,b){for(var o in b)a[o]=b[o];return a},
create:function(a,b,c,d){var g=this,o=(d||document).createElement(a);g.set(o,b);g.set(o.style,c);return o},
add:function(a,b,c,d){var o=b.tagName?b:this.create(b,c,d);a.appendChild(o);return o},
text:function(a,b){var g=this,o=g.tag(a,b);return o.textContent||o.text},
field:function(a,b){
    var g=this,o=g.form
    g.add(o,'label',{innerHTML:a})
    g.add(o,'input',{name:a,value:b})
    g.add(o,'br')
},
meta:function(){
    var g=this,o=g.tags('meta'),i,v=[],n
    for(i=0;i<o.length;i++){
        n=o[i].name.toLowerCase()
        switch(n){
            case 'description':g.field('PageDescription',o[i].content);break
            case 'keywords':g.field('PageKeywords',o[i].content);break
        }
    }
},
init:function(){
    var g=this,o,db
    db=g.get('bookmark-script').src
    db=db.substr(0,db.lastIndexOf('/'))
o=g.form=g.create('form',{method:'post',target:'_blank',action:db+'/Link?CreateDocument',name:'_Link'})
    g.field('PageUrl',location.href)
    g.field('PageTitle',document.title)
    g.field('PageH1',g.tag('h1')?g.tag('h1').innerHTML:'Undefined')
    g.meta()
    g.add(g.tag('body'),o)
    o.submit()
}
}

Bookmark.init()

The code behind the Link form

I wanted a new document to be created when the information on this PageUrl was not yet present. However, If I already bookmarked this page, instead of creating a new document it would just update the existing document. So the @formulas in the $$Return field take care of that. Using @DbLookup, I try to obtain the DocumentUniqueId of it, if exists. If a document is found, I set the SaveOptions for this document to "0", preventing it to being saved. Next I update the fields of the existing document. Finally, the $$Return field opens the existing document in read mode:

The $$Return field

@If(@IsDocBeingSaved; ""; @Return(""));
a:=@DbLookup("":""; ""; "lookup-links"; PageUrl; 2);
b:=@If(@IsError(a); @return("[/"+@WebDbName+"/0/"+@Text(@DocumentUniqueID)+"]"); a);

Field SaveOptions:="0";
@SetDocField(b; "PageTitle"; PageTitle);
@SetDocField(b; "PageH1"; PageH1);
@SetDocField(b; "PageDescription"; PageDescription);
@SetDocField(b; "PageKeywords"; PageKeywords);
"[/"+@WebDbName+"/0/"+b+"]"

One of my next goals: add a download section to my pages so that you can download the sample files and databases. Enjoy.

Star rating

0%

Comments

To add a comment, log in or register as new user. It's free and safe.