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: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);
});
@ZER0
ZER0 / gist:5255953
Created March 27, 2013 16:52
Change a label of a context-menu item based on selection
let cm = require("sdk/context-menu");
let item = cm.Item({
label: "My Menu Item",
context: cm.SelectionContext()
});
require("sdk/selection").on("select", function(){
item.label = this.text
})
@ZER0
ZER0 / gist:5267608
Last active April 26, 2023 17:26
Find all the CSS rules applied to a specific element; and check if a CSS property for a specific element is defined in the stylesheet – not inline style. Notice that is not like `getComputedStyle`, that returns the calculated properties for a specific element.
var proto = Element.prototype;
var slice = Function.call.bind(Array.prototype.slice);
var matches = Function.call.bind(proto.matchesSelector ||
proto.mozMatchesSelector || proto.webkitMatchesSelector ||
proto.msMatchesSelector || proto.oMatchesSelector);
// Returns true if a DOM Element matches a cssRule
var elementMatchCSSRule = function(element, cssRule) {
return matches(element, cssRule.selectorText);
};
@ZER0
ZER0 / gist:5548355
Last active December 17, 2015 04:09
let { Button } = require("sdk/ui");
let button = Button({
id: "my-button",
// assuming automatically `data` folder for "./"
image: "./beer.png",
// required as tooltip and overflow
label: "My Button",
type: "checked"
});
diff --git a/lib/sdk/deprecated/unit-test-finder.js b/lib/sdk/deprecated/unit-test-finder.js
index 0d13265..c2268d5 100644
--- a/lib/sdk/deprecated/unit-test-finder.js
+++ b/lib/sdk/deprecated/unit-test-finder.js
@@ -55,8 +55,20 @@ TestFinder.prototype = {
function(suite) {
// Load each test file as a main module in its own loader instance
// `suite` is defined by cuddlefish/manifest.py:ManifestBuilder.build
- var loader = Loader(module);
- var module = cuddlefish.main(loader, suite);
@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:10024431
Created April 7, 2014 17:19
Add-on SDK: to Certificate Viewer window
let { events: windowEvents } = require('sdk/window/events');
let { on } = require('sdk/event/core');
let { filter } = require('sdk/event/utils');
let ready = filter(windowEvents, ({type}) => type === 'DOMContentLoaded');
let certificateWindows = filter(ready, ({target}) =>
target.document.documentElement.mozMatchesSelector('dialog#certDetails'));
on(certificateWindows, 'data', ({target: window}) => {
@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()));