[PATCH] javascript: bookmarks, aka bookmarklets

Alexander Kellett lypanov at kde.org
Mon Jan 20 21:12:08 GMT 2003


This nano patchess add the ability to use javascript: 
style location bar urls (sometimes known as bookmarklets) 
to konqueror. Its a revision of the last one addressing
most of the problems with it. The khtml_part side of
the code has been commited already.

As an example, try "javascript:window.resizeTo(800,600)"
Konqueror will "magically" resize itself.  This URL can
then be bookmarked giving a simple way to set the window 
size. Several thousand other uses for this are available.

still TODO:
  ask waldo if some kiosk option should be added
  display a security warning when executing a javascript: url (with tickbox to disable)
  come up with some really useful bookmarklets

I'm aiming to commit the patch by the end of the week
and shall be doing some more thorough testing mid-week.

Some feedback would be nice :)

mvg,
Alex who's gonna miss Angel this time around

-- 
"[...] Konqueror open source project. Weighing in at less than
            one tenth the size of another open source renderer"
Apple,  Jan 2003 (http://www.apple.com/safari/)
-------------- next part --------------
Index: konq_mainwindow.cc
===================================================================
RCS file: /home/kde/kdebase/konqueror/konq_mainwindow.cc,v
retrieving revision 1.1110
diff -u -p -B -w -r1.1110 konq_mainwindow.cc
--- konq_mainwindow.cc	18 Jan 2003 15:57:25 -0000	1.1110
+++ konq_mainwindow.cc	20 Jan 2003 21:04:04 -0000
@@ -429,7 +429,25 @@ void KonqMainWindow::openURL( KonqView *
   if ( !view  && !req.newTab)
     view = m_currentView; /* Note, this can be 0L, e.g. on startup */
 
-  if ( view )
+  m_pendingBookmarklet = QString::null;
+  
+  if ( url.protocol() == "javascript" ) {
+    // TODO - is the decode_string correct usage or does url.url() do that?
+    m_pendingBookmarklet = KURL::decode_string( url.url().right(url.url().length() - 11) );
+    if ( view->url().isEmpty() || !view || !view->part() || !view->part()->inherits("KHTMLPart") ) 
+    {
+      abortLoading();
+      setLocationBarURL( url.prettyURL() );
+      serviceType = "text/html";
+      url = "about:blank";
+    }
+    else 
+    {
+      executePendingBookmarklet( view );
+      return;
+    }
+  }
+  else if ( view )
   {
     if ( view == m_currentView )
     {
@@ -1655,9 +1673,25 @@ void KonqMainWindow::slotSetStatusBarTex
    // Does nothing here, see konq_frame.cc
 }
 
+void KonqMainWindow::executePendingBookmarklet( KonqView * view )
+{
+  assert( view );
+  kdWarning(1202) << "KonqMainWindow::executePendingBookmarklet" << endl;
+  disconnect(this, SIGNAL( executeScript(const QString &) ), 0, 0);
+  connect(this, SIGNAL( executeScript(const QString &) ), 
+          view->part(), SLOT( executeScript(const QString &) ));
+  emit executeScript( m_pendingBookmarklet );
+  m_pendingBookmarklet = QString::null;
+}
+
 void KonqMainWindow::slotViewCompleted( KonqView * view )
 {
   assert( view );
+
+  if ( !m_pendingBookmarklet.isNull() )  {
+    executePendingBookmarklet( view );
+    return;
+  }
 
   // Need to update the current working directory
   // of the completion object everytime the user
Index: konq_mainwindow.h
===================================================================
RCS file: /home/kde/kdebase/konqueror/konq_mainwindow.h,v
retrieving revision 1.383
diff -u -p -B -w -r1.383 konq_mainwindow.h
--- konq_mainwindow.h	18 Jan 2003 17:34:34 -0000	1.383
+++ konq_mainwindow.h	20 Jan 2003 21:04:04 -0000
@@ -299,6 +299,7 @@ public:
 signals:
   void viewAdded( KonqView *view );
   void viewRemoved( KonqView *view );
+  QVariant executeScript( const QString & );
 
 public slots:
   void slotCtrlTabPressed();
@@ -524,6 +525,8 @@ private:
 
   void popupNewTab(bool infront, bool openAfterCurrentPage);
 
+  void executePendingBookmarklet( KonqView * view );
+
   /**
    * Tries to find a index.html (.kde.html) file in the specified directory
    */
@@ -678,6 +679,8 @@ private:
 
   bool m_bBackRightClick;
 
+  QString m_pendingBookmarklet;
+  
   static bool s_preloaded;
   static KonqMainWindow* s_preloadedWindow;
 


More information about the kfm-devel mailing list