Skip to content
Chris edited this page Apr 14, 2017 · 4 revisions

Welcome to the Alfred wiki!

For developers

Add a new module

For example you want to add a MyScrollUpModule. At first add 2 files to Alfred/scripts/modules folder. The first file is MyScrollUpModule.js. It looks like this:

addModule(new Module("MyScrollUpModule", function() {
    //specify a new action
    var scrollUp = new Action("MyScrollUp", 0, globalCommonState);
    scrollUp.act = function() {
        callContentScriptMethod("MyScrollUp", {}, function (params) {
            //callback after content script method with parameter "param"
            if (typeof params !== 'undefined' && params.hasOwnProperty("content")) {
                //voice response
                say(params.content);
            }
        });
    };
    //add action to module
    this.addAction(scrollUp);
}));

Here we define a new module "MyScrollUpModule" where we initialize a new action "MyScrollUp". The act function of this action will be called after the later specified voice command. In this function we add the callContentScriptMethod where you can call methods which run on the content script level of this application

To define these content script methods you have to add the second file MyScrollUpModule.csm.js

addContentScriptMethod(
    new ContentScriptMethod("MyScrollUp", function() {
        //get scroll status
        var scrollHeight = window.innerHeight * 0.7;
        var scrollPosVertical = window.scrollY;
        if(scrollPosVertical > 0) {
            //scrolling up will not reach the top of the page -> scroll up
            $("html, body").animate({scrollTop: scrollPosVertical - scrollHeight}, 1000);
        } else {
            //Position of scrolling is on top of the page -> alert
            //translate searches in Alfred/scripts/languages/enValues.json for the key "notifyCannotScrollUp"
            showMessage({content: translate("notifyCannotScrollUp"), centered: true});
            return({content: translate("sayCannotScrollUp")}); //param for callback
        }
    })
);

The name of the content script method has to be equal to the string in the call of the callContentScriptMethod function.

To specify the voice command, you have to add your action to the modules language files. You find them in Alfred/scripts/languages. Add the module to the JSON Array "modules" like this:

"modules" : [
	...
	{
		"module" : "MyScrollUpModule",
		"name" : "My Scroll Up Module"
	},
	...
]

Add your action to the JSON Array "actions" like this:

"actions" : [
	...
	{
		"action" : "MyScrollUp",
		"name" : "My Scroll Up",
		"commands" : ["my scroll up", "my scrolling command"]
	},
	...
]

At the end, you have to add the MyScrollUpModule.js file in the Alfred/scripts/modules/moduleList.js and the MyScrollUpModule.csm.js file in the Alfred/manifest.json.

Now you can say "my scroll up" or "my scrolling command" and your browser should scroll up. (if possible)

Clone this wiki locally