Skip to content

Instantly share code, notes, and snippets.

@ispedals
Created June 23, 2013 21:25
Show Gist options
  • Save ispedals/5846591 to your computer and use it in GitHub Desktop.
Save ispedals/5846591 to your computer and use it in GitHub Desktop.
Addon SDK usage

#Addon SDK usage

##Add widget to addon bar

require('sdk/widget').Widget({
    id: '...',
label: '...', //context label
	content: '...', //text label
	width: 100,
	onClick: function() {
		...
	}
});

##Make HTTP GET request

require('sdk/request').Request({
	url: '...',
	onComplete: function (response) {
		let contents=response.text;
	}
}).get();

##Access chrome

let {Cc, Ci, Cu} = require('chrome');

##Log to console Prints to both error console, and command prompt if the addon is run using cfx run

function log() {
	console.log(Array.prototype.slice.call(arguments));
}

log(...);

##Call function of addon disable (shutdown, disable, uninstall, etc)

require('sdk/system/unload').when(function(){
	...
});

##Execute function periodically

require('sdk/timers').setInterval(func, duration);

##Append to file Example append to existing file C:\Users\Owner\Desktop\urls.txt Can't use sdk/io/file.open because it does not have an append mode The current set flags in myFile.init require the file to exist

let {Cc,Ci} = require('chrome');
let file = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsILocalFile);
file.initWithPath('C:\\Users\\Owner\\Desktop\\urls.txt');
let myFile = Cc['@mozilla.org/network/file-output-stream;1'].createInstance(Ci.nsIFileOutputStream);
myFile.init(file, 0x02 | 0x10, 0666, 0); //these flags enable various functionality
let text = '...';
myFile.write(text, text.length);
myFile.close();

##Tabs Get the URL of the current tab

require('sdk/tabs').activeTab.url;

Open URL in a new tab

require('sdk/tabs').open(url);

##Downloads Get count of active downloads (both global and private)

let {Cc, Ci, Cu} = require('chrome');
let download_manager = Cc['@mozilla.org/download-manager;1'].getService(Ci.nsIDownloadManager);
let download_counts = download_manager.activeDownloadCount + download_manager.activePrivateDownloadCount;

Sample nsIDownloadProgressListener implementation for nsIDownloadManager.addListener

function Download_Listener() {
	this.onSecurityChange = function(prog, req, state, dl) { };
	this.onProgressChange = function(prog, req, prog, progMax, tProg, tProgMax, dl) { };
	this.onStateChange = function(prog, req, flags, status, dl) { };

	this.onDownloadStateChange = function(state, dl) {
		...
	};
}

Cc['@mozilla.org/download-manager;1'].getService(Ci.nsIDownloadManager).addListener(new Download_Listener());

##Ctypes Call a Win32 function Example calling setThreadExecutionState, described at http://msdn.microsoft.com/en-us/library/windows/desktop/aa373208%28v=vs.85%29.aspx

const DWORD = ctypes.uint32_t; //DWORD is uint32
var setThreadExecutionState = function (state) {
	Cu.import('resource://gre/modules/ctypes.jsm');
	var lib = ctypes.open('kernel32.dll');
	var setThreadExecutionState = lib.declare('SetThreadExecutionState', ctypes.winapi_abi, DWORD, DWORD);
	setThreadExecutionState(state);
	lib.close();
};

Declare a struct type Example declaring struct_LASTINPUTINFO for the user32 function GetLastInputInfo and setting its size

const DWORD = ctypes.uint32_t; //DWORD is uint32
const struct_LASTINPUTINFO = new ctypes.StructType( 'PLASTINPUTINFO', [ { 'cbSize' : ctypes.unsigned_int}, { 'dwTime' : DWORD} ] );
let info = struct_LASTINPUTINFO();
info.cbSize = struct_LASTINPUTINFO.size;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment