Binding searchengines

Bind a searchengine to a shortcut, the keyword is the keyword that would be used in the addressbar.

#!javascript

[
  // Bind google to '\g'
  { shortcut : "\\\\g",  name : "google",    keyword : "g" },
  // Bind wikipedia to '\w'
  { shortcut : "\\\\w",  name : "wikipedia", keyword : "w" }
].forEach(function(v) {
    bind(v.shortcut, function() {
      var response = io.prompt(v.name + ":");
      if (response)
        execute("open " + v.keyword + " " + response);
    });
});

Custom statusbar

Mostly the default statusbar but implemented as a userscript

#!javascript

signals.connect("statusBarChange", function(wv, data) {
    // uri
    var uri = util.markupEscape(wv.uri);
    if (data.ssl == "trusted")
        uri = "<span foreground='#0f0'>" + uri + "</span>";
    else if (data.ssl == "untrusted")
        uri = "<span foreground='#f00'>" + uri + "</span>";

    // history
    var statusText = " ";
    if (data.canGoBack || data.canGoForward) {
        statusText += "[";
        if (data.canGoBack)
            statusText += "+";
        if (data.canGoForward)
            statusText += "-";
        statusText += "] ";
    }

    // Scroll
    var adjustment = wv.parent.vadjustment;

    var lower = adjustment.lower;
    var upper = adjustment.upper - adjustment.pageSize + lower;
    var value = adjustment.value;

    if (upper == lower)
        statusText += "[all]";
    else if (value == lower)
        statusText += "[top]";
    else if (value == upper)
        statusText += "[bot]";
    else
    {
        statusText += "[";
        var perc = Math.round(value * 100 / upper);
        if (perc < 10)
            statusText += " ";
        statusText += perc + "%]";
    }
    // Number of tab
    statusText += "[" + (tabs.number + 1) + "/" + tabs.length + "]";

    // Progressbar
    var progressBarLength = 20;
    var progress = wv.progress;
    if (progress > 0 && progress < 1)
    {
        var len = Math.round(progress * progressBarLength);
        statusText += "[" + Array(len + 1).join("=") + Array(progressBarLength - len + 1).join(" ") + "] ";
    }

    gui.uriLabel.label = uri;
    gui.statusLabel.label = statusText;
    return true;
});

Dump tabs

Dump current tab uris to a file, when called from commandline the argument will be used as the path.

#!javascript

var defaultPath = "/tmp/dwb_tabs";

function dump_tabs(response) {
  var i, uris = new String();
  var path = defaultPath;
  if (response.arg !== undefined && ! (/^\s*$/.test(response.arg))) {
    path = response.arg;
  }
  for (i=0; i<tabs.length; i++) {
    uris += tabs.nth(i).uri + "\n";
  }
  try {
    io.write(path, "w", uris);
    timerStart(1, function() {
      io.notify("Tabs dumped");
      return false;
    });
  }
  catch (e) {
    io.error(e);
  }
}
bind("ed", dump_tabs, "dump_tabs");

Duplicate tabs

Remove duplicate tabs

//!javascript

function removeDuplicates() {
    var uris = [];
    for (var i=tabs.length-1; i>=0; --i)
    {
        var uri = tabs.nth(i).uri;
        if (uris.fastIndexOf(uri) == -1)
            uris.push(uri);
        else
            execute((i+1) + "close_tab");
    }
}

bind(null, removeDuplicates, "remove_duplicates");

Edit Bookmarks/Quickmarks

Edit bookmarks and quickmarks in an external editor.

//!javascript


var reload = function(command, file)
{
    var shellCommand = "sh -c 'xterm -e vim " + file + "'";
    system.spawn(shellCommand).then(function() {
        execute(command);
    });
};

bind(null, reload.bind(null, "reload_bookmarks", data.bookmarks), "edit_bookmarks");
bind(null, reload.bind(null, "reload_quickmarks", data.quickmarks), "edit_quickmarks");

Enforce ssl

Force ssl on websites, domains can be defined in hosts-array, tld matches all top level domains, e.g. google.tld will match google.com, google.com.ag, google.uk, …

//!javascript

var hosts = [
    "twitter.com",
    "google.tld"
];

var tldRegex = new RegExp("\.tld$");

var stripped = hosts.map(function(v) { return v.substr(0, v.indexOf(".")); });

var matchHost = function(host)
{
    var h, base, name, i;

    base = util.domainFromHost(host);
    name = base.substring(0, base.indexOf("."));

    for (i=hosts.length-1; i>=0; --i)
    {
        h = hosts[i];
        if (tldRegex.test(h))
        {
            if (name == stripped[i])
                return true;
        }
        else if (h == base)
            return true;
    }
    return false;
};

var navigationCallback = function(wv, frame, request)
{
    var id;

    if (wv.mainFrame != frame)
        return false;

    id = wv.getPrivate("id", this);

    if (matchHost(request.message.uri.host))
    {
        if (!id)
        {
            id = wv.connect("resource-request-starting", function(w, frame, resource, request)
            {
                if (request && request.message.uri.scheme == "http")
                    request.uri = "https" + request.uri.substring(4);
            });
            wv.setPrivate("id", id, this);
        }
    }
    else if (id > 0)
    {
        wv.disconnect(id);
        wv.setPrivate("id", 0, this);
    }
    return false;
};

signals.connect("navigation", navigationCallback.bind(this));

Reload Extensions

Reload extensions that are handled by dwbem.

//!javascript

var reloadExtensions = function()
{
    extensions.disableAll();
    include(data.configDir + "/userscripts/extension_loader.js");
}
bind(null, reloadExtensions, "reload_extensions");

Youtube

Automatically play videos in youtube, uses mplayer and youtube-dl.

#!javascript

var regex = new RegExp("https?://www.youtube.com/watch\\?(.*&)*v=.*");

signals.connect("navigation", function (wv, frame, request) {
  if (wv.mainFrame == frame && regex.test(request.uri))
    system.spawn("sh -c 'mplayer \"$(youtube-dl -g " + request.uri + ")\"'");
  return false;
});

Play videos in mplayer with a shortcut, plays links if the mouse hovers over a youtube link, otherwise it uses the current url.

#!javascript

var hoverUri;
var regex = new RegExp("https?://www.youtube.com/watch\\?(.*&)*v=.*");

signals.connectWebView("hovering-over-link", function(w, title, uri) {
  hoverUri = uri;
});

bind("v", function() {
  var uri = hoverUri || tabs.current.uri;
  if (regex.test(uri)) {
    system.spawn("sh -c 'mplayer \"$(youtube-dl -g " + uri + ")\"'");
    io.notify("Playing in mplayer");
  }
});

Try to play html5 videos on youtube whenever possible

//!javascript

signals.connect("navigation", function(wv, frame, request) {
    if ( frame == wv.mainFrame                          &&
         request.message.uri.host == "www.youtube.com"  &&
         !(/&html5=1/.test(request.uri)))
    {
        wv.loadUri(request.uri + "&html5=1");
        return true;
    }
});