Skip to content

Instantly share code, notes, and snippets.

View ZER0's full-sized avatar

Matteo Ferretti ZER0

  • Amsterdam, Netherlands
View GitHub Profile
@ZER0
ZER0 / gist:9746736
Created March 24, 2014 18:56
Open a HTML Document dialog in Add-on SDK
/* Open a plain dialog */
const { getActiveTab, setTabURL } = require('sdk/tabs/utils');
const { open } = require('sdk/window/helpers');
const { data } = require('sdk/self');
const url = data.url('foo.html');
open("", {
features: {
// add `contentType` to page-mod:
// https://bugzilla.mozilla.org/show_bug.cgi?id=788224
// current API:
// all the http/https urls
PageMod({
include: '*', // can take string
contentScript: 'console.log("hello")'
@ZER0
ZER0 / gist:10637946
Last active August 29, 2015 13:59
Add-on SDK: Reverse the tabs' order in the current window
const { ActionButton } = require("sdk/ui/button/action");
const { browserWindows } = require("sdk/windows");
let button = ActionButton({
id: "reverse-tabs",
label: "Reverse Tab Order",
icon: {
// The new APIs use "./" as shortcut for data folder, you don't need
// to require `self` for that purpose.
"16": "./16.png",
@ZER0
ZER0 / gist:10708822
Created April 15, 2014 06:59
Add-on SDK: take a screenshot of the active tab's content
const { window: { document } } = require('sdk/addon/window');
const { getTabContentWindow, getActiveTab } = require('sdk/tabs/utils');
const { getMostRecentBrowserWindow } = require('sdk/window/utils');
const canvas = document.createElementNS('http://www.w3.org/1999/xhtml', 'canvas');
document.documentElement.appendChild(canvas);
function captureActiveTab() {
let contentWindow = getTabContentWindow(getActiveTab(getMostRecentBrowserWindow()));
@ZER0
ZER0 / gist:10ef3b4b491c1dac3d70
Created August 7, 2014 16:25
ToggleButton with Global and Tab scope checked status
const { ToggleButton } = require('sdk/ui/button/toggle');
let globalToggle = ToggleButton({
id: 'my-global-toggle',
label: 'global function',
icon: './foo.png',
onChange: function() {
// delete the window state for the current window,
// automatically set when the user click on the button
this.state('window', null);
@ZER0
ZER0 / gist:d49c9cd35ad4dd08389c
Last active August 29, 2015 14:19
highlight a word in a page
let word = "Mozilla";
// set the walker for non-empty text node
let walker = document.createTreeWalker(
document.body,
NodeFilter.SHOW_TEXT,
{ acceptNode: (node) => node.nodeValue.trim().length && NodeFilter.FILTER_ACCEPT }
);
// collecting the nodes
function timespan(start, end) {
if (typeof end === "undefined") {
end = start;
start = Date.now();
}
if (start !== null && typeof start === "object" && start.getTime) {
start = start.getTime();
}
JPM [info] Starting jpm test on addon-sdk
Creating XPI
JPM [info] XPI created at /var/folders/tx/91xwh51562l5_4xcqjst9bvw0000gn/T/@addon-sdk-0.1.18.xpi (1173ms)
Created XPI at /var/folders/tx/91xwh51562l5_4xcqjst9bvw0000gn/T/@addon-sdk-0.1.18.xpi
JPM [info] Creating a new profile
Shumway is registered
Running tests on Firefox 42.0a1/Gecko 42.0a1 (Build 20150805030208) ({ec8030f7-c20a-464f-9b0e-13a3a9e97384}) under darwin/x86_64.
console.error: addon-sdk:
JPM [error] Message: TypeError: FakeCu is not a constructor
Stack:
@ZER0
ZER0 / rulers.js
Created October 6, 2015 16:10
Example of highligther based on CanvasHighlighter
function RulersHighlighter2(highlighterEnv) {
CanvasHighlighter.call(this, highlighterEnv);
}
RulersHighlighter2.prototype = extend(CanvasHighlighter.prototype, {
typeName: "RulersHighlighter",
textStep: 100,
graduationStep: 5,
stepScale: 1,
@ZER0
ZER0 / gist:5209412
Last active December 15, 2015 05:29
JEP window.postMessage for Add-ons
// main.js
PageMod({
onAttach: function(worker) {
worker.addEventListener("message", function(event){
if (event.origin === "good.com") // page script
event.source.postMessage(event.data + " addon!", "good.com");
else if(event.origin === worker.origin) // content script
event.source.postMessage("content script, hello!", worker.origin);
});