K-Meleon

Macros are a unique feature of K-Meleon. You can use them to customize the browser or expand its features. Macros can be activated from toolbars, menus, right-click menus, keyboard shortcuts, events, and even other macros. Macros have several advantages over larger extensions:

  • Macros are easier for end-users to write.
  • They can be stored in a single file.
  • They don't have to be compiled or compressed.
  • They can be opened, read, and modified in any text editor.


Download macros

WARNING: Responsibility for macros lies with their respective owners.

You can find many user-created macros in the extensions forum. There are also many older macros in the Macro Library. You can install a macro by saving it to your K-Meleon/Macros folder or the /profiles/<your.profile>/macros/ folder. You can open your macros folder by navigating to Configuration > Macros.

Some macros are part of larger extension packages. See the extensions page for more information.


Manage macros

  • Open your macros folder to view or edit macros, by going to Edit > Configuration > Macros in the menu. You can edit macros in any text editor.
  • Toggle entire macro modules in the preferences by going to Edit > Preferences (F2) >> K-Meleon Plugins > Macro Extension and clicking a macro's name.
  • You can store custom macros in your profile. Create the folder K-Meleon/profiles/<your.profile>/macros/, copy your custom macros from the default macro folder above, and then paste them into your new profile folder. Any individual macro (not the whole .kmm file) in your profile will override the version in the default folder. The profile folder is easier to transport between K-Meleon installations. You can move your profile folder from the old install to the new one, and keep your custom macros.


Modules & individual macros

K-Meleon macro modules (.kmm) are text files that contain one or more individual macros. Within a module file, each named section enclosed in {brackets} is a separate macro that can be called from toolbars, menus, other macros, etc. For nearly all places in K-Meleon where a name is required, the name of the individual macro is needed. The one exception is the preferences dialog where module names are used to manage installed modules. Perhaps confusingly both the modules and the individual macros are referred to as "macros."


Writing a simple macro

K-Meleon macros are more approachable than other web browser extension systems. This section will walk you through the steps to build one. Below is the entire code for a simple "Hello world!" macro. To run this in K-Meleon:

  1. Copy the code below into a text editor.
  2. Save it as helloWorld.kmm and note that you may have to set the file type to all files when you save if your editor adds .txt to the end of your file name. That file is now a K-Meleon macro module (.kmm) and can be loaded the next time you start K-Meleon.
  3. Put the file into the K-Meleon/Macros/ folder or your /profiles/<your.profile>/macros/ folder.
  4. Restart K-Meleon.
  5. Press CTRL+ALT+SHIFT+H or any key combination you define in the macro. And you should see a "Hello world!" alert box appear.
# Anything on a line starting with "#" is a comment.
# Comments do not affect how your code works.


# This is our first section of code. Each section works as an individual macro, and 
# each section can be called by any other macro in any macro file, so choose 
# descriptive and unique names for them
_HelloWorld_BuildMenu{
# You can replace this keyboard accelerator with any shortcut you like:
        setaccel("CTRL ALT SHIFT H","macros(HelloWorld)");
}


# This section is the macro that our keyboard accelerator calls. 
# Notice that the name is the same.
HelloWorld{
        alert("Hello world!");
}

# ---------------------------------------------------------------------
# K-Meleon keeps lists of macros to trigger at different times.
# $OnInit is the list of macros that K-Meleon will run before it 
# processes the user's preferences. This is a good time to create menus 
# because it won't override any of the user's choices. The 
# '_HelloWorld_BuildMenu' macro must be in at least one of K-Meleon's
# lists for the browser to build the menu. Because our other macro
# section is triggered by a command, it does not need to be added.
$OnInit=$OnInit."_HelloWorld_BuildMenu;";

You'll notice that the module has two separate macros. First, _HelloWorld_BuildMenu creates an accelerator to trigger a macro. Next, HelloWorld defines that macro.

It can be difficult to remember so many keyboard shortcuts. Most software reminds users of frequent keyboard shortcuts from within its menus. K-Meleon will automatically list your keyboard shortcut if you create a menu option, so let's add a menu option to the browser:

_HelloWorld_BuildMenu{
        # The number at the end of the line for menu items can indicate relative 
        # position as below (0 or -1) or it can specify the exact order of several items.
        setmenu("&View",macro,"Hello World","HelloWorld",0);
        setaccel("CTRL ALT SHIFT H","macros(HelloWorld)");
}


HelloWorld{
        alert("Hello world!");
}


$OnInit=$OnInit."_HelloWorld_BuildMenu;";
# You likely won't need the line below for simple macros.
# $macroModules is a variable where K-Meleon keeps a list of all active
# macros. It can be checked by any macro to resolve dependencies. It's 
# good practice to use it with any macro especially if you share it 
# with the community.
$macroModules=$macroModules."HelloWorld;"; 

You should now see a menu entry at or near the top of the View menu. If it's not visible, close and restart K-Meleon to reload your macro. All the added code does is position a menu item that calls the HelloWorld macro. Placing new menus at 0 stacks them at the top of the menu and placing new menus at -1 stacks them at the bottom of the menu.

Next, let's make the above macro into something more useful. Some web pages use tiny fonts, so let's create an accessibility option to give those pages an 18 point font like large-print books:

_HelloWorld_BuildMenu{
        setmenu("&View",macro,"Large-print","HelloWorld",0);
        setaccel("CTRL ALT SHIFT H","macros(HelloWorld)");
}


HelloWorld{
        alert("Large print activated for current page until refreshed.");
        # This will place custom CSS code into the current page.
        # "!important" allows this CSS style to override styles on a web page
        injectCSS("html, body, main, p, blockquote, table, tr, td, ol li, ul li {   
        font-size: 18pt !important; }");
}


$OnInit=$OnInit."_HelloWorld_BuildMenu;";
$macroModules=$macroModules."HelloWorld;";

Aside from the name changes, we've only added one line of code to inject some CSS. The line of CSS will increase the font of most parts of a page to 18pt. This will have the most impact on fluid pages like the K-Meleon wiki or Wikipedia. Now that the macro has a specific function, it doesn't make so much sense to call it HelloWorld. If you wanted to rename parts of the macro it helps to make use of variables. It also helps to use comments not just as messages but to separate your code into chunks. This way if you return the macro after a while you'll still have a decent idea of what you're reading.

# ---------------------------------------------------------------------
# Hello World 
#
# UTF-8  K-Meleon Macros 
# http://kmeleon.sourceforge.net/wiki/MacroLanguage2
#
# ---------------------------------------------------------------------
$_HelloWorld_menu="&View";
$_HelloWorld_name="Large-print"; 
$_HelloWorld_macro="HelloWorld";
$_HelloWorld_accel="CTRL ALT SHIFT H";


# ---------------------------------------------------------------------
# Macro Sections
# ---------------------------------------------------------------------
_HelloWorld_BuildMenu{
        setmenu($_HelloWorld_menu,macro,$_HelloWorld_name,$_HelloWorld_macro,0);
        setaccel($_HelloWorld_accel,"macros(HelloWorld)");
}

HelloWorld{
        alert("Large print activated for current page until refreshed.");
        injectCSS("html, body, main, p, blockquote, table, tr, td, ol li, ul li {   
        font-size: 18pt !important; }");
}


# ---------------------------------------------------------------------
# Startup
# ---------------------------------------------------------------------
$OnInit=$OnInit."_HelloWorld_BuildMenu;";
$macroModules=$macroModules."HelloWorld;";

If you decide to share your macro on the forums, read over the MacroGuidelines especially the section about naming variables.

And congrats on building your first macro!


Launching macros with keyboard shortcuts

The above module is stand-alone. It works without additional files or settings. It's also possible to launch a macro from K-Meleon's keyboard accelerator shortcuts or other macros.

To launch the HelloWorld macro above from your keyboard shortcuts:

  1. Make sure that "helloWorld.kmm" or another module containing HelloWorld{} is in your Macros folder.
  2. Click Configuration > Accelerators and your default text editor will open the configuration file for your keyboard accelerator shortcuts.
  3. Type the line below onto the bottom line of your configurations:
    CTRL ALT SHIFT H = macros(HelloWorld)
  4. Save the document and restart K-Meleon.

Custom accelerators allow you to write smaller extensions. Because the config file creates the keyboard shortcut, you don't have to build one in your macro. The macro can be shortened down to:

HelloWorld{
        alert("Hello world!");
}


Launching macros from menus

K-Meleon can also launch macros from menus and toolbars. Macros handle many of the default menu options in K-Meleon. To add custom menu options for macros:

  1. Go to Configuration > Menus to open menus.cfg in a text editor.
  2. Create your item using the syntax described in the default config file.
  3. Save menus.cfg and re-start K-Meleon.

For example, the code below would add the HelloWorld macro to K-Meleon's right-click menu on any page. Try adding it and then right-clicking this page:

!Nav {
  HelloWorld=macros(HelloWorld)
}

Nav is the topmost menu section that has navigation options. The exclamation point adds the HelloWorld entry inline rather than replacing items or popping out a submenu. If you want to add an item to the main menu, just choose an area that suits your macro. For example, you could replace Nav with Network above.

You can find more information about custom configurations on the ConfigFiles page. You can find the names of K-Meleon's menus in the default menus.cfg located at K-Meleon/browser/defaults/settings.


Launching macros from toolbar buttons

  1. Launch K-Meleon and go to Edit > Configuration > Toolbars to open the toolbar configuration used by your current skin in a text editor. Do not edit this file yet.
    • If you are using the Default skin or another 75-style skin that does not store toolbars.cfg in the skins folder, you can save a copy of toolbars.cfg directly into your profile folder.
    • If you're using a classic-style skin (like Klassic) that has its own toolbars.cfg you have several options. The most reliable is to create a folder in your profile with the exact same name, so for example to create new buttons for the Klassic theme you'd create a Profiles/your.profile/skins/Klassic/ folder and save a copy of toolbars.cfg in this folder. This is slightly more complicated but will also work reliably for 75-style themes.
    • And if you do decide to edit the original toolbars.cfg, be sure to save a copy. An easy way to store copies of configs is to simply rename the original file from toolbars.cfg to toolbars.txt and leave it in the same location.
  2. Once you have a copy of toolbars.cfg in your profile, open it, and scroll all the way down to the bottom.
  3. Copy and paste the template below. Keep reading for an explanation on how to edit it.
Custom Toolbar{

!Button Name{
macros(HelloWorld)
Tooltip for button.
}

}

The first line of this template creates a new toolbar called Custom Toolbar. The next line is the name of the first button. The ! tells K-Meleon to display the name as text. The third line is what launches a macro. You can replace HelloWorld with any macro of your choice. Be sure to use the name of the macro section and not the entire module's file name. Next, there is a tooltip that pops up when your mouse hovers over the icon. You can add custom images below this; refer to the other buttons in your theme for examples or check out the skinning tutorial for more information. Finally, you'll close the button and the toolbar with a pair of curly braces. Save toolbars.cfg and restart K-Meleon to use your macro from the new button.


Macro references

Legacy Macros

The documentation for the old macro language and a library of user-submitted legacy macros are archived on the wiki. Even older versions of K-Meleon stored macros within a configuration file called macros.cfg, but as of version 1.0 all functions of macros.cfg were moved into the "main.kmm" module.

K-Meleon

(c) 2000-2010 kmeleonbrowser.org. All rights reserved.
design by splif.