question about SelectorContext, functions and cllipboard

103 views
Skip to first unread message

Carlos Pantelides

unread,
Jan 10, 2012, 8:40:14 AM1/10/12
to mozilla-labs-jetpack
Hi:

I am trying to make an extension for applying a transformation to a
link before following it. It works fine but there are two problems:

First one, functions: I couldnt refactor the code "some
transformation" to a function. I've tried following the example
(https://addons.mozilla.org/en-US/developers/docs/sdk/1.3/dev-guide/
addon-development/implementing-simple-addon.html) that makes so, but
when I change to SelectorContext, the rules seems to change.

Why do I want to refactor if its working fine? Because I want to add
another behavior, "copy transformed link to clipboard", so I would
like to use a function instead of copypasting the transformation, a
good practice.

Second one, clipboad: I couldnt access the clipboad following the
instructions (https://addons.mozilla.org/en-US/developers/docs/sdk/1.3/
packages/addon-kit/docs/clipboard.html). I tried adding

let clipboard = require("clipboard");

but when I tried to access it from contentScript, I have the same
result as with the functions in the first problem, so I think that
they are related.

Any hint or help will be greatly appreciated.


var contextMenu = require("context-menu");

exports.main = function(options, callbacks) {
console.log(options.loadReason);
var menuItem = contextMenu.Item({
label: "follow transformed link",

context: contextMenu.SelectorContext("a"),

contentScript: 'self.on("click", function (node, data) {' +
' console.log("Item clicked!");' +
' console.log(node);' +
' url = some transformation to node;' +
' window.location.href = url;' +
'});'
});
};

Carlos Pantelides
-----------------
http://seguridad-agile.blogspot.com/

Hernan Rodriguez Colmeiro

unread,
Jan 25, 2012, 10:17:21 AM1/25/12
to mozilla-la...@googlegroups.com
On Tue, Jan 10, 2012 at 10:40, Carlos Pantelides
<carlos.p...@gmail.com> wrote:
> First one, functions: I couldnt refactor the code "some
> transformation" to a function. I've tried following the example
> (https://addons.mozilla.org/en-US/developers/docs/sdk/1.3/dev-guide/
> addon-development/implementing-simple-addon.html) that makes so, but
> when I change to SelectorContext, the rules seems to change.
>
> Why do I want to refactor if its working fine? Because I want to add
> another behavior, "copy transformed link to clipboard", so I would
> like to use a function instead of copypasting the transformation, a
> good practice.
>

I couldn't follow what is the code that is not working, nor you
specific problem with the example page. Could you give some more
details about this problem?

> Second one, clipboad: I couldnt access the clipboad following the
> instructions (https://addons.mozilla.org/en-US/developers/docs/sdk/1.3/
> packages/addon-kit/docs/clipboard.html). I tried adding
>
> let clipboard = require("clipboard");
>
> but when I tried to access it from contentScript, I have the same
> result as with the functions in the first problem, so I think that
> they are related.

In this case, if what you get is the following error:

ReferenceError: clipboard is not defined

That is because from inside the content script you do not have
permissions to access the variables and modules declared outside it.
You should send a message to the main thread and then from the
listener in the main thread access the clipboard. Here you'll find
some pointers about how to do that:
https://addons.mozilla.org/en-US/developers/docs/sdk/1.3/dev-guide/addon-development/content-scripts/using-postmessage.html

As a final note, try to use a contentScriptFile not the code inline in
a contentScript as it might not pass AMO revision and is way harder to
write having to escape everything ;)

Hernán

Carlos Pantelides

unread,
Feb 8, 2012, 12:45:41 PM2/8/12
to mozilla-labs-jetpack
Thank you, Hernan,

> I couldn't follow what is the code that is not working,
> nor you specific problem with the example page.
> Could you give some more details about this problem?

Given the code that I posted before and the example from the doc, I
want to transform it to the following code:


var contextMenu = require("context-menu");

exports.main = function(options, callbacks) {
console.log(options.loadReason);
var menuItem = contextMenu.Item({
label: "clear link",
context: contextMenu.SelectorContext("a.l"),
contentScript: 'self.on("click", function (node, data) {' +
' console.log(node); ' +
' self.postMessage(node); ' +
'});',
onMessage: function (item) {
console.log('cleaning "' + item + '"');
clean(item);
}
});
};

function clean(node) {
return node;
}

even tried replacing

' self.postMessage(node); ' +
with
' var text = node; ' +
' self.postMessage(text); ' +

The output is:

info: startup
info:
http://www.google.com/url?sa=t&rct=j&q=hola&source=web&cd=1&ved=0CDQQFjAA&url=http%3A%2F%2Fwww.hola.com%2F&ei=57MyT-adGdCUtwf-m52sBw&usg=AFQjCNHwZtGcMMYHk7wtvddeSf1n3gm1RA&cad=rja
error: An exception occurred.
Traceback (most recent call last):
File "resource://jid1-btslvogbfswhpw-at-jetpack-api-utils-lib/
timer.js", line 66, in notifyOnTimeout
this._callback.apply(null, this._params);
File "resource://jid1-btslvogbfswhpw-at-jetpack-api-utils-lib/
content/worker.js", line 80, in emitter
emit.apply(scope, params);
File "resource://jid1-btslvogbfswhpw-at-jetpack-api-utils-lib/
events.js", line 153, in _emit
return this._emitOnObject.apply(this, args);
File "resource://jid1-btslvogbfswhpw-at-jetpack-api-utils-lib/
events.js", line 183, in _emitOnObject
listener.apply(targetObj, params);
File "javascript:self.on("click", function (node, data)
{ console.log(node); self.postMessage(node); });", line 1, in null
File "resource://jid1-btslvogbfswhpw-at-jetpack-api-utils-lib/
content/worker.js", line 164, in postMessage
JSON.parse(JSON.stringify(data)));
[Exception... "Component is not available" nsresult: "0x80040111
(NS_ERROR_NOT_AVAILABLE)" location: "JS frame ::
resource://jid1-btslvogbfswhpw-at-jetpack-api-utils-lib/securable-module.js
-> resource://jid1-btslvogbfswhpw-at-jetpack-api-utils-lib/content/worker.js
:: postMessage :: line 164" data: no]

Jeff Griffiths

unread,
Feb 10, 2012, 10:49:29 PM2/10/12
to mozilla-la...@googlegroups.com
It looks to me like node is a DOM element. You can't pass one of those
into postMessage, you can only pass along JSON-serializable data:

https://addons.mozilla.org/en-US/developers/docs/sdk/1.4/dev-guide/addon-development/content-scripts/using-port.html#json_serializable

You cannot pass that node out of the content script, you can only
operate on it *in* the content script, or pass data from that node out
via postMessage. You cannot pass through a reference to the node
itself, as this is not allowed by the security model of the SDK.

Jeff

> --
> You received this message because you are subscribed to the Google Groups "mozilla-labs-jetpack" group.
> To post to this group, send email to mozilla-la...@googlegroups.com.
> To unsubscribe from this group, send email to mozilla-labs-jet...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mozilla-labs-jetpack?hl=en.
>

Carlos Pantelides

unread,
Feb 13, 2012, 8:59:08 AM2/13/12
to mozilla-labs-jetpack
Thank you, Jeff.

Now my script is almost working!!

The only problem left is that I can not change the location as
self:postMessage does not return what onMessage returns and window is
not defined in onMessage nor clean()

How can I return or set a value to contentScript?

Is there another way to change the content of a tab, open a new tab or
window?


Charli



context: contextMenu.SelectorContext("a.l"),
contentScript: 'self.on("click", function (node,data) {' +
' var text = new String(node); ' +
' window.location.href =
self.postMessage({"url":text.trim()}); ' +
'});',
onMessage: function (item) {
return clean(item.url);
}


function clean(url) {
return url;
}

Carlos Pantelides

unread,
Feb 13, 2012, 12:46:25 PM2/13/12
to mozilla-labs-jetpack
RESOLVED: I've learn how to accomplish what I needed. Thank you all
for the help


onMessage: function (item) {
switch(item.f) {
case "clipboard":
let clipboard = require("clipboard");
clipboard.set(clean(item.url));
break;
case "here":
var tabs = require("tabs");
tabs.activeTab.url = clean(item.url);

break;
case "tab":
var tabs = require("tabs");
tabs.open(clean(item.url));
break;
case "window":
var windows = require("windows").browserWindows;
windows.open(clean(item.url));
break;
default:
console.log(item.f);
}
}

Carlos Pantelides

unread,
Feb 13, 2012, 1:12:14 PM2/13/12
to mozilla-labs-jetpack

Jeff Griffiths

unread,
Feb 13, 2012, 9:42:48 PM2/13/12
to mozilla-la...@googlegroups.com
Great! Glad you figured you out!

On Mon, Feb 13, 2012 at 10:12 AM, Carlos Pantelides
<carlos.p...@gmail.com> wrote:
> https://addons.mozilla.org/en-US/firefox/addon/nogoogle/

Reply all
Reply to author
Forward
0 new messages