29 kwietnia 2013

Firefox addon - run calc.exe

Przydatna rzecz - wtyczka dla Firefox-a, choć praca nad nią była dość niewygodna. W planach miałem, zbudowanie czegoś podobnego do wtyczki RefControl, która kontroluje co jest wysyłane jako HTTP Referer do strony. Na razie jednak, zacząłem od czegoś prostszego. Do zbudowania wtyczki, posłużył mi Add-on builder:
Poniższy przykład nie robi nic użytecznego. Dopisuje do każdej strony "Hello world" (na jej początku), zapisuje w konsoli vendor-a, a po naciśnięciu ikony widgetu otwiera w nowej zakładce stronkę dupa.pl i uruchamia kalkulator (to jest wersja pod Windows). Poniżej przydatne linki, z dokumentacją do wykorzystanych bibliotek:
Dość często miała problemy ze zdiagnozowanie przyczyny, dlaczego wtyczka się nie "kompiluje". A gdy "kompilacja" już się powiodła, często znikała ikona mojego widget-u. Rozwiązaniem drugiego problemu, było częste zmienianie jego identyfikatora. Bez wątpienia, potrzebna jest pewna wprawa, żeby pisanie następnych mogło odbywać się sprawniej.
// The main module of the niegodziwyberu Add-on.

// Modules needed are `require`d, similar to CommonJS modules.
// In this case, creating a Widget that opens a new tab needs both the
// `widget` and the `tabs` modules.
var Widget = require("widget").Widget;
var tabs = require('tabs');
var { Cc, Ci } = require('chrome');

exports.main = function() {
    // https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/system.html#vendor
    var system = require("sdk/system");
    console.log("Console print, vendor = " + system.vendor);

    // https://addons.mozilla.org/en-US/developers/docs/sdk/latest/dev-guide/tutorials/modifying-web-pages-url.html
    // Import the page-mod API
    var pageMod = require("sdk/page-mod");

    // Create a page mod
    // It will run a script whenever a ".pl" URL is loaded
    // The script replaces the page contents with a message
    pageMod.PageMod({
        include: "*.pl",
        contentScript: 'content.document.body.innerHTML = ' +
                       '"<h1>Hello World</h1>" + content.document.body.innerHTML;'
    });

    // https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIProcess#See_also
    // ZACZEŁO DZIAŁAĆ, PO PRZENIESIENIU TUTAJ
    // create an nsIFile for the executable
    var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);

    // create an nsIProcess
    var process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);

    // https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/widget.html
    new Widget({
        // Mandatory string used to identify your widget in order to
        // save its location when the user moves it in the browser.
        // This string has to be unique and must not be changed over time.
        // DA SIĘ COŚ ZROBIĆ, ŻEBY TEGO CAŁY CZAS NIE POPRAWIAĆ?
        id: "niegodziwier-widget-7",

        // A required string description of the widget used for
        // accessibility, title bars, and error reporting.
        label: "NiegodziwyBeru Widget",

        // An optional string URL to content to load into the widget.
        // This can be local content or remote content, an image or
        // web content. Widgets must have either the content property
        // or the contentURL property set.
        //
        // If the content is an image, it is automatically scaled to
        // be 16x16 pixels.
        contentURL: "http://www.mozilla.org/favicon.ico",

        // Add a function to trigger when the Widget is clicked.
        onClick: function(event) {
            // Open a new tab in the currently active window.
            tabs.open("http://dupa.pl");

            // Open calc application
            file.initWithPath("c:\\windows\\system32\\calc.exe");
            process.init(file);
            var args = [];
            process.run(false, args, args.length);
        }
    });

};

Brak komentarzy:

Prześlij komentarz