Xfce Wiki

Sub domains
 

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
midori:tutorial [2014/03/01 13:37] – [Working with (web) views] axlrosemidori:tutorial [2014/03/04 18:36] – [Add an app menu item] axlrose
Line 159: Line 159:
  
 <code vala> <code vala>
-    // in tab_added +namespace Sandcat { 
-    tab.context_menu.connect (add_menu_items);+    private class Manager : Midori.Extension { 
 +  
 +        internal Manager () { 
 +            GLib.Object (name: _("Furry example extension"), 
 +                         description: _("Take care of petting cats"), 
 +                         version: "0.1" + Midori.VERSION_SUFFIX, 
 +                         authors: "Jane Doe <email@domain.tld>"); 
 +            activate.connect (this.activated); 
 +            deactivate.connect (this.deactivated); 
 +        }
  
-    void add_menu_items (WebKit.HitTestResult hit_test_result, Midori.ContextAction menu) { +        void tab_added (Midori.Browser browser, Midori.Tab tab) { 
-        if ((hit_test_result.context & WebKit.HitTestResultContext.SELECTION) == 0+            tab.context_menu.connect (add_menu_items); 
-            return;+        }
  
-        var action = new Gtk.Action ("NotesCopySnippet", "Copy _snippet to notes", null, null); +        void add_menu_items (WebKit.HitTestResult hit_test_result, Midori.ContextAction menu) { 
-        action.activate.connect (() => { +            if ((hit_test_result.context & WebKit.HitTestResultContext.SELECTION) == 0) 
-            stdout.printf ("my menu item rocks\n"); +                return; 
-        }); +  
-        menu.add (action); +            var action1 = new Gtk.Action ("NotesCopySnippet", "Copy _snippet to notes", null, null); 
-        var submenu = new Midori.ContextAction ("NotesPaste", "Paste N_otes", null, null); +            var action2 = new Gtk.Action ("NotesPaste", "Paste N_otes", null, null); 
-        for (int i = 0; i < 10; i++) { +            action1.activate.connect (() => { 
-            submenu.add (new Gtk.Action ("NotesSnippet%d".format (i), "Snippet #%i".format (i), null, null);+                stdout.printf ("my menu item1 rocks\n"); 
 +            }); 
 +            action2.activate.connect (() => { 
 +                stdout.printf ("my menu item2 rocks\n"); 
 +            }); 
 +            menu.add (action1); 
 +            menu.add (action2); 
 +             
 +            //Add group of submenu 
 +            var submenu = new Midori.ContextAction("HaveSubmenu", "Have S_ubmenu", null, null); 
 +            var listsubmenu = new List<Gtk.Action>(); 
 +            for (int i = 0; i < 10; i++) 
 +                listsubmenu.append(new Gtk.Action("Submenu%d".printf (i), "Submenu #%i".printf (i), null, null)); 
 + 
 + 
 +            listsubmenu.foreach ((entry) => { 
 +                entry.activate.connect(() => { 
 +                    stdout.printf ("I am a submenu\n"); 
 +                }); 
 +                submenu.add(entry); 
 +            }); 
 +            menu.add (submenu); 
 +        } 
 + 
 +        void browser_added (Midori.Browser browser) { 
 +             foreach (var tab in browser.get_tabs ()) 
 +                tab_added (browser, tab); 
 +             browser.add_tab.connect (tab_added); 
 + 
 +        } 
 + 
 +        void activated (Midori.App app) { 
 +            foreach (var browser in app.get_browsers ()) 
 +                browser_added (browser); 
 +            app.add_browser.connect (browser_added); 
 +        } 
 + 
 +        void browser_removed (Midori.Browser browser) { 
 +            foreach (var tab in browser.get_tabs ()) { 
 +                browser.add_tab.disconnect (tab_added); 
 +            } 
 +        } 
 + 
 +        void deactivated () { 
 +            var app = get_app (); 
 +            app.add_browser.disconnect (browser_added); 
 +            foreach (var browser in app.get_browsers ()) 
 +                browser_removed (browser);
         }         }
-        menu.add (submenu); 
     }     }
-</code>+}
  
 +public Midori.Extension extension_init () {
 +    return new Sandcat.Manager ();
 +}
 +</code>
 +In the Web page try to select a word or a sentence right click and you are going to see three menu item which were added, click on one of them and you will see the magic in the terminal :) . 
 ====== Add an app menu item ====== ====== Add an app menu item ======
 +<code vala>
 +namespace Sandcat {
 +    private class Manager : Midori.Extension {
 +        internal Manager () {
 +            GLib.Object (name: _("Furry example extension"),
 +                         description: _("Take care of petting cats"),
 +                         version: "0.1" + Midori.VERSION_SUFFIX,
 +                         authors: "Jane Doe <email@domain.tld>");
 +            activate.connect (this.activated);
 +            deactivate.connect (this.deactivated);
 +        }
 +        void tool_menu_populated (Gtk.Menu menu) {
 +            var item = new Gtk.MenuItem.with_label("i am here");
 +            menu.add(item);
 +            item.show();
 +        }
  
-whatever+        void browser_added (Midori.Browser browser) { 
 +            browser.populate_tool_menu.connect(tool_menu_populated); 
 +        }
  
 +        void activated (Midori.App app) {
 +            foreach (var browser in app.get_browsers ())
 +                browser_added (browser);
 +            app.add_browser.connect (browser_added);
 +        }
 +
 +        void deactivated () {
 +            var app = get_app ();
 +            app.add_browser.disconnect (browser_added);
 +        }
 +    }
 +}
 +
 +public Midori.Extension extension_init () {
 +    return new Sandcat.Manager ();
 +}
 +</code>
 +
 +Here you are enjoy, it is easy, isn't it ? :-)
 ====== Modify resources or webpages loaded ====== ====== Modify resources or webpages loaded ======
  
Line 190: Line 287:
  
 <code vala> <code vala>
-    // in browser_added +namespace Sandcat { 
-    var button = new Gtk.Button.with_label ("Click Here!"); +    private class Manager : Midori.Extension { 
-    browser.statusbar.pack_start (button, falsefalse, 3); +  
-    button.show (); +        internal Manager () { 
-</code>+            GLib.Object (name: _("Furry example extension"), 
 +                         description: _("Take care of petting cats"), 
 +                         version: "0.1" + Midori.VERSION_SUFFIX, 
 +                         authors: "Jane Doe <email@domain.tld>"); 
 +            activate.connect (this.activated); 
 +            deactivate.connect (this.deactivated); 
 +        }
  
 +        void browser_added (Midori.Browser browser) {
 +            var button = new Gtk.Button.with_label ("Click Here!");
 +            var click_counter = 0;
 +            browser.statusbar.pack_start (button, false, false, 3);
 +            button.clicked.connect (() => {
 +                button.label = "You Clicked me (%d) time(s)".printf (++click_counter);
 +            });
 +            button.show ();
 +        }
 +
 +        void activated (Midori.App app) {
 +            foreach (var browser in app.get_browsers ())
 +                browser_added (browser);
 +            app.add_browser.connect (browser_added);
 +        }
 +
 +        void deactivated () {
 +            var app = get_app ();
 +            app.add_browser.disconnect (browser_added);
 +        }
 +    }
 +}
 +
 +public Midori.Extension extension_init () {
 +    return new Sandcat.Manager ();
 +}
 +</code>
 +Enjoy How much time you click :-), you can add any Gtk widget in this StatusBar.
 ====== Add a panel ====== ====== Add a panel ======
  
Line 256: Line 387:
      Test.add_func ("/extensions/furry/purr", test_furry_purr);      Test.add_func ("/extensions/furry/purr", test_furry_purr);
 } }
 +</code>
 +
 +With a simple example it will be like this:
 +<code vala>
 +void test_furry_purr () {
 +    assert (1 == 1);
 +}
 + 
 +public void extension_test () {
 +     Test.add_func ("/extensions/furry/purr", test_furry_purr);
 +}
 +
 +void main (string[] args) {
 +    Test.init (ref args);
 +    extension_test();
 +    Test.run ();
 +}</code>
 +The output could look like this.
 +<code bash>
 +/extensions/furry/purr: OK
 </code> </code>