javascript: bookmarks, aka bookmarklets

Alexander Kellett lypanov at kde.org
Wed Jan 8 19:20:27 GMT 2003


These two nano patches add the ability to use javascript: 
style location bar urls (sometimes known as bookmarklets) 
to konqueror.

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.

David Faure mentioned possible security issues with this
patch, but, well, I can't really think of any :)

My TODO list: (to add to the one in the src)
   make sure that the executeScript is just ignored if javascript is disabled
   ask waldo if some kiosk option should be added
   make it possible to use javascript urls without needing a current page
      - i.e, somehow reuse the about:blank code
   display a security warning when executing a javascript: url (with tickbox to disable)
   come up with some really useful bookmarklets

mvg,
Alex who's gonna miss the start of buffy cus he got carried away with the hack :( ... :)

-- 
"[...] 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: khtml_part.cpp
===================================================================
RCS file: /home/kde/kdelibs/khtml/khtml_part.cpp,v
retrieving revision 1.790
diff -u -p -B -w -r1.790 khtml_part.cpp
--- khtml_part.cpp	8 Jan 2003 06:18:52 -0000	1.790
+++ khtml_part.cpp	8 Jan 2003 17:53:56 -0000
@@ -4989,6 +4989,11 @@ void KHTMLPart::preloadScript(const QStr
     khtml::Cache::preloadScript(url, script);
 }
 
+QVariant KHTMLPart::slotExecuteScript( const QString &script )
+{
+   return executeScript(script);
+}
+
 QCString KHTMLPart::dcopObjectId() const
 {
   QCString id;
Index: khtml_part.h
===================================================================
RCS file: /home/kde/kdelibs/khtml/khtml_part.h,v
retrieving revision 1.200
diff -u -p -B -w -r1.200 khtml_part.h
--- khtml_part.h	29 Dec 2002 23:35:02 -0000	1.200
+++ khtml_part.h	8 Jan 2003 17:53:56 -0000
@@ -839,6 +839,8 @@ public slots:
 
   QCString dcopObjectId() const;
 
+  QVariant slotExecuteScript(const QString &);
+
 private slots:
 
   /**
-------------- next part --------------
Index: konq_mainwindow.h
===================================================================
RCS file: /home/kde/kdebase/konqueror/konq_mainwindow.h,v
retrieving revision 1.376
diff -u -p -B -w -r1.376 konq_mainwindow.h
--- konq_mainwindow.h	9 Dec 2002 16:57:31 -0000	1.376
+++ konq_mainwindow.h	8 Jan 2003 19:22:46 -0000
@@ -290,6 +290,7 @@ public:
 signals:
   void viewAdded( KonqView *view );
   void viewRemoved( KonqView *view );
+  QVariant executeScript(const QString &);
 
 public slots:
   void slotCtrlTabPressed();
Index: konq_mainwindow.cc
===================================================================
RCS file: /home/kde/kdebase/konqueror/konq_mainwindow.cc,v
retrieving revision 1.1092
diff -u -p -B -w -r1.1092 konq_mainwindow.cc
--- konq_mainwindow.cc	9 Dec 2002 18:07:46 -0000	1.1092
+++ konq_mainwindow.cc	8 Jan 2003 19:22:46 -0000
@@ -425,6 +426,23 @@ void KonqMainWindow::openURL( KonqView *
   if ( !view  && !req.newTab)
     view = m_currentView; /* Note, this can be 0L, e.g. on startup */
 
+  if ( url.protocol() == "javascript" ) {
+    // TODO - the following line should correctly check if the current view is a khtmlpart
+    if ( view && view->part() && view->part()->inherits("KHTMLPart") )
+    {
+      connect(this, SIGNAL( executeScript(const QString &) ), 
+              view->part(), SLOT( slotExecuteScript(const QString &) ));
+      emit executeScript(url.path());
+    }
+    else 
+    {
+      KMessageBox::error(0, i18n("Javascript URLs work only for already opened HTML pages"));
+      serviceType = "text/html";
+      url = KURL();
+    }
+    return;
+  }
+
   if ( view )
   {
     if ( view == m_currentView )


More information about the kfm-devel mailing list