Kwwika ceased operation on 01/06/2011. Please migrate to Pusher

API‎ > ‎JavaScript‎ > ‎How to...‎ > ‎

Use Kwwika in a bookmarklet or GreaseMonkey Script

This page is a work in progress

The best way to dynamically load any script, that we've found, is to use LABJS. The writers of LABJS have also provided a handy little script that also dynamically includes LABJS. The code below shows how to dynamically include LABJS and then dynamically include the Kwwika JavaScript API. Once this is included you can then use Kwwika as you would normally do.

Notes:
// HOWTO: load LABjs itself dynamically!
// inline this code in your page to load LABjs itself dynamically, if you're so inclined.

(function (global, oDOC, handler) {
    var head = oDOC.head || oDOC.getElementsByTagName("head");

    function LABjsLoaded() {
        doStuff();
    }

    // loading code borrowed directly from LABjs itself
    setTimeout(function () {
        if ("item" in head) { // check if ref is still a live node list
            if (!head[0]) { // append_to node not yet ready
                setTimeout(arguments.callee, 25);
                return;
            }
            head = head[0]; // reassign from live node list ref to pure node ref -- avoids nasty IE bug where changes to DOM invalidate live node lists
        }
        var scriptElem = oDOC.createElement("script"),
            scriptdone = false;
        scriptElem.onload = scriptElem.onreadystatechange = function () {
            if ((scriptElem.readyState && scriptElem.readyState !== "complete" && scriptElem.readyState !== "loaded") || scriptdone) {
                return false;
            }
            scriptElem.onload = scriptElem.onreadystatechange = null;
            scriptdone = true;
            LABjsLoaded();
        };
        scriptElem.src = "http://example.com/LAB.js";
        head.insertBefore(scriptElem, head.firstChild);
    }, 0);

    // required: shim for FF <= 3.5 not having document.readyState
    if (oDOC.readyState == null && oDOC.addEventListener) {
        oDOC.readyState = "loading";
        oDOC.addEventListener("DOMContentLoaded", handler = function () {
            oDOC.removeEventListener("DOMContentLoaded", handler, false);
            oDOC.readyState = "complete";
        }, false);
    }
})(window, document);

function doStuff()
{
    document.write = function(){}; // we need to remove some bad code from the Kwwika JS API that does a document.write.
    $LAB.script("https://js.kwwika.com/beta/api.js")
    .wait(function()
    {
        var kc = kwwika.Service.connect(
            {apiKey:"YOUR_API_KEY"});
    });
};