Knowing your own metadata

133 zobrazení
Přeskočit na první nepřečtenou zprávu

ScroogeMcPump

nepřečteno,
23. 6. 2008 0:14:0423.06.08
komu: greasemonkey-users
At <http://wiki.greasespot.net/Metadata_block>, it says, "Other Keys
[such as] @author, @version, ... are ignored by the Greasemonkey
extension, but they can be read by human beings or other code."

My question is, how? How can a script be made aware of its own
metadata? Does anyone have sample code for this? And if so: Why
doesn't someone with access put it up on the wiki?

Stefan Petrea

nepřečteno,
23. 6. 2008 2:22:1723.06.08
komu: greasemon...@googlegroups.com
You mean something like this ?

177 function getMetaKeywords() {
178 var i;
179 var mt = window._content.document.getElementsByTagName("META");
180 for(i=0;i<mt.length;i++) {
181 var meta_tag=mt.item(i);
182 if( meta_tag.name.toLowerCase() == 'keywords' ) {
183 return meta_tag.content;
184 };
185 }
186 }

--

Stefan Petrea
homepage : http://perlhobby.googlecode.com/
mobile phone : +40732070179

Anthony Lieuallen

nepřečteno,
23. 6. 2008 10:09:1423.06.08
komu: greasemon...@googlegroups.com
On 6/23/2008 12:14 AM, ScroogeMcPump wrote:
>> At <http://wiki.greasespot.net/Metadata_block>, it says, "Other Keys
>> ...can be read by human beings or other code."

> My question is, how? How can a script be made aware of its own
> metadata?

It doesn't necessarily say that _user scripts_ can read them. More
that, say, us.o could use them. Or a script that auto-updates could XHR
it, parse it, and see if the (version) number has changed since last update.

ScroogeMcPump

nepřečteno,
23. 6. 2008 11:26:3523.06.08
komu: greasemonkey-users
On Jun 23, 2:22 am, Stefan Petrea <stefan.pet...@gmail.com> wrote:
> You mean something like this ?

No. This isn't about HTML <meta> data; it's about Greasemonkey
metadata in the // ==UserScript== block.

ScroogeMcPump

nepřečteno,
23. 6. 2008 11:45:0923.06.08
komu: greasemonkey-users
On Jun 23, 10:09 am, Anthony Lieuallen <arant...@gmail.com> wrote:

> It doesn't necessarily say that _user scripts_ can read them.

Well, maybe user scripts SHOULD be able to read them.

> Or a script that auto-updates could XHR it, parse it, and

... compare the XHR'd metadata to what? The data to compare it to is
locked away in what the JS interpreter thinks is a comment; that's why
the famous (but now broken) auto-update script forces everyone to
duplicate all this data in a JS object.

I was looking to incorporate a simpler auto-update routine into my own
script, but it seems ridiculous that there's a perfectly good spot for
this info that's totally inaccessible TO the script itself. (My
script's users already consider the process of installing Firefox,
installing Greasemonkey, and then installing my script to be
cumbersome; I think asking them to install yet another script would be
too much for some of their widdle minds to take.)

And on a related subject, how CAN I incorporate an auto-update routine
into my script without it artificially inflating my download stats on
us.o?

Stefan Petrea

nepřečteno,
23. 6. 2008 12:52:1723.06.08
komu: greasemon...@googlegroups.com
ok js with enough priviledges can read any files on the hdd.
there is a function called readFile here
http://perlhobby.googlecode.com/svn/trunk/firefox_toolbar_version_0.5/tuttoolbar_unziped/chrome/content/tuttoolbar.js
you can use that if you want but you will have to build the path to gm_scripts.
I believe it's possible for it to read it's own userscript block.

--

ScroogeMcPump

nepřečteno,
23. 6. 2008 13:45:3123.06.08
komu: greasemonkey-users
On Jun 23, 12:52 pm, Stefan Petrea <stefan.pet...@gmail.com> wrote:

> there is a function called readFile
> [...]
> you will have to build the path to gm_scripts.

Okay, then that brings up two more questions:
[1] Is an ordinary Greasemonkey script going to have sufficient
privileges to run code like this?
[2] Is an ordinary Greasemonkey script going to be able to figure out
its own location on the hard drive?
(If the answer to [1] is "yes", then I think [2] is information that
Gm should automatically make available to the script. OTOH, if the
contents of the // ==UserScript== block were automatically made
available to the script, none of this rigamarole would be necessary.)

Anthony Lieuallen

nepřečteno,
23. 6. 2008 13:56:5423.06.08
komu: greasemon...@googlegroups.com
On 6/23/2008 1:45 PM, ScroogeMcPump wrote:
> [1] Is an ordinary Greasemonkey script going to have sufficient
> privileges to run code like this?

No.

> [2] Is an ordinary Greasemonkey script going to be able to figure out
> its own location on the hard drive?

Because of 1, no.


P.S.

========================================================
var metadata=<>
// ==UserScript==
// @name MetaData Grabber
// @namespace http://arantius.com/misc/greasemonkey/
// @description Get the @UserScript metadata for this script...
// @include *
// ==/UserScript==
</>.toString();

alert(metadata);
========================================================

ScroogeMcPump

nepřečteno,
23. 6. 2008 21:14:3623.06.08
komu: greasemonkey-users
On Jun 23, 1:56 pm, Anthony Lieuallen <arant...@gmail.com> wrote:

> var metadata=<>

Dang, that's clever! I had no idea JS had a "here document" syntax!
I even downloaded the ECMA spec, and still found no mention of it - so
it's not surprising that no one thought of doing it that way before!

It'll take some tweaking with split() and stuff, but that'll
definitely work! Thanks!

Anthony Lieuallen

nepřečteno,
24. 6. 2008 9:55:5924.06.08
komu: greasemon...@googlegroups.com
On 6/23/2008 9:14 PM, ScroogeMcPump wrote:
> On Jun 23, 1:56 pm, Anthony Lieuallen <arant...@gmail.com> wrote:
>> var metadata=<>
> Dang, that's clever! I had no idea JS had a "here document" syntax!

This is a sort of bastardization of E4X [1]. It's really a bare XML
node, which has text as its only child, so .toString() gets that text out.

[1] http://developer.mozilla.org/en/docs/E4X

> It'll take some tweaking with split() and stuff, but that'll
> definitely work! Thanks!

The GM code that parses the metadata is JS, so you can start from there.

Dan Phiffer

nepřečteno,
24. 6. 2008 10:35:2324.06.08
komu: greasemon...@googlegroups.com

On Jun 23, 2008, at 9:14 PM, ScroogeMcPump wrote:

> On Jun 23, 1:56 pm, Anthony Lieuallen <arant...@gmail.com> wrote:
>
>> var metadata=<>
>
> Dang, that's clever! I had no idea JS had a "here document" syntax!
> I even downloaded the ECMA spec, and still found no mention of it - so
> it's not surprising that no one thought of doing it that way before!

Yeah, nice hack Anthony!


> It'll take some tweaking with split() and stuff, but that'll
> definitely work! Thanks!

Here's an attempt at parsing it further:

var metadata = {};
<>
// ==UserScript==
// @name Introspect
// @namespace http://phiffer.org/
// @description Simple introspection demo


// @include *
// ==/UserScript==

</>.toString().split('@').forEach(function(item) {
var key = item.match(/^(\w+)/);
if (key) metadata[key[1]] = item.match(/\s+(.+)/)[1];
});

John Plsek

nepřečteno,
24. 6. 2008 10:51:1424.06.08
komu: greasemon...@googlegroups.com
I don't get it, a script knowing it's own meta data ... you wrote the
script, you wrote the meta data, what's the point of complex coding to
get hard coded information much of which can be made useless information
by changing the includes/excludes in "Manage Scripts" page?

Vikas Pandey

nepřečteno,
24. 6. 2008 11:45:2724.06.08
komu: greasemon...@googlegroups.com
Nope, John,

I think he has a point. If we want to display something from our
metadata, then we can use such a variable name in our main script if it
has been possible for a variable to pick metadata.

often, we might like to show the name of the script or its description
or its location (where to download it from), or its rev num at the
places where the script is run. Sort of advertising.

Currently, one has to change manually everywhere, and if one misses
changing at some places, then the display will go wrong.

--
V

Matt Sargent

nepřečteno,
24. 6. 2008 12:20:2124.06.08
komu: greasemon...@googlegroups.com
I think you missed his point. The metadata that the script has access to
may not necessarily reflect what is seen in the script manager. I can
add and remove include/excludes, and these changes will not be revealed
by this technique. And I can go in and edit the script description, and
it would be detected by this technique, but not in the script manager.
Basically, you're dealing with a mix of both dynamic and static data,
and if a coder doesn't realize this, they're going to get some
unexpected results.

ScroogeMcPump

nepřečteno,
24. 6. 2008 13:20:0324.06.08
komu: greasemonkey-users
On Jun 24, 12:20 pm, Matt Sargent <matt.sarg...@earthlink.net> wrote:

> Basically, you're dealing with a mix of both dynamic and static data

True, but if you read back to my original post, all I'm interested in
at this point are keys like @date or @version, which are not only not
manipulated by Gm, but totally ignored by it.

(I still think Gm should create a standard object where ALL of this
info - static, dynamic, or nonstandard - would be readable by the
script itself; but this hack will do nicely for what I want.)

Stefan Petrea

nepřečteno,
24. 6. 2008 14:41:5724.06.08
komu: greasemon...@googlegroups.com
On 06-23 10-45, ScroogeMcPump wrote:
>
> On Jun 23, 12:52 pm, Stefan Petrea <stefan.pet...@gmail.com> wrote:
>
> > there is a function called readFile
> > [...]
> > you will have to build the path to gm_scripts.
>
> Okay, then that brings up two more questions:
> [1] Is an ordinary Greasemonkey script going to have sufficient
> privileges to run code like this?

I guess that's a TIAS.
If not,use the greasemonkey extension compiler,or modify the
greasemonkey.js to have this readFile function which it will
use in the script as the extension is capable of running this
kind of code.

> [2] Is an ordinary Greasemonkey script going to be able to figure out
> its own location on the hard drive?

Again see the first answer and also check out the greasemonkey compiler
script.

> (If the answer to [1] is "yes", then I think [2] is information that
> Gm should automatically make available to the script. OTOH, if the
> contents of the // ==UserScript== block were automatically made
> available to the script, none of this rigamarole would be necessary.)
> >

--

Stefan Petrea

nepřečteno,
24. 6. 2008 15:31:2724.06.08
komu: greasemon...@googlegroups.com

Can't GM be modified for this or are you looking to use this with
an existing version of GM ?

John Plsek

nepřečteno,
24. 6. 2008 22:20:2224.06.08
komu: greasemon...@googlegroups.com
Ahh, fair enough then. I must admit I didn't read that, and I concede
that having the info only once as meta data is far neater - and besides,
it's a cool solution too ;)

ScroogeMcPump

nepřečteno,
25. 6. 2008 11:43:1225.06.08
komu: greasemonkey-users
On Jun 24, 3:31 pm, Stefan Petrea <stefan.pet...@gmail.com> wrote:

> Can't GM be modified for this or are you looking to use this with
> an existing version of GM ?

The latter, definitely. (Try a Ctrl-F on this thread for "widdle".)

ScroogeMcPump

nepřečteno,
25. 6. 2008 11:43:1925.06.08
komu: greasemonkey-users
On Jun 24, 2:41 pm, Stefan Petrea <stefan.pet...@gmail.com> wrote:

> > > there is a function called readFile
> [...]
> I guess that's a TIAS.

I trust Anthony's "no" verdict on this - plus his hack means it's no
longer necessary.

Johan Sundström

nepřečteno,
1. 7. 2008 3:14:5101.07.08
komu: greasemon...@googlegroups.com
On Tue, Jun 24, 2008 at 7:20 PM, ScroogeMcPump <gasbudd...@yahoo.com> wrote:
> (I still think Gm should create a standard object where ALL of this
> info - static, dynamic, or nonstandard - would be readable by the
> script itself; but this hack will do nicely for what I want.)

Both a convenience GM API to get script headers and the whole script
source code as a string are things I want to add to Greasemonkey, but
I haven't quite come up with how I want them to behave with respect to
@require yet (at the moment those are not treated as headers, but they
should, to allow a library to require other libraries and resources
itself). There is some old discussion on the topic on greasemonkey-dev
if you search for GM_headers.

Prior to that happening, I would suggest using this hack instead of
those proposed, as they bug out if your @include or @exclude urls
contain a bare &, which isn't all unlikely:

var headers = parseHeaders(<><![CDATA[
// ==UserScript==
// @name Header parser
// @namespace http://ecmanaut.googlecode.com/
// @description A quick E4X hack
]]></>.toXMLString().split(/[\r\n]+/).filter(/\/\/ @/));

function parseHeaders(all) {
var headers = {}, name, value;
for each (var line in all) {
[line, name, value] = line.match(/\/\/ @(\S+)\s*(.*)/);
headers[name] = value;
}
return headers;
}

--
/ Johan Sundström, http://ecmanaut.blogspot.com/

ScroogeMcPump

nepřečteno,
1. 7. 2008 13:39:4601.07.08
komu: greasemonkey-users
On Jul 1, 3:14 am, "Johan Sundström" <oyas...@gmail.com> wrote:

> I would suggest using this hack instead of those proposed

Okay, I get how CDATA makes it better, but with that in place, why do
we need toXMLString() instead of plain toString()? The XML code
really isn't relevant to the parsing and will get stripped out anyway,
so why bother to pass it in?

Also, in what I'm working on, I fold .split() and .filter() into the
parsing function - so that it can be reused elsewhere on data from
other sources with minimal duplication of code.

> There is some old discussion on the topic on greasemonkey-dev
> if you search for GM_headers.

BTW, +1 on GM_headers, AND +1 on @xpath, too, FWIW from a person with
zero dev experience here; I know @xpath would've simplified the coding
of my (one and only public) script... :-)

Johan Sundström

nepřečteno,
1. 7. 2008 18:32:3101.07.08
komu: greasemon...@googlegroups.com
On Tue, Jul 1, 2008 at 7:39 PM, ScroogeMcPump <gasbudd...@yahoo.com> wrote:
> On Jul 1, 3:14 am, "Johan Sundström" <oyas...@gmail.com> wrote:
>> I would suggest using this hack instead of those proposed
>
> Okay, I get how CDATA makes it better, but with that in place, why do
> we need toXMLString() instead of plain toString()? The XML code
> really isn't relevant to the parsing and will get stripped out anyway,
> so why bother to pass it in?

Feel free to shave off those three characters; the two amount to the
exact same thing. (Personally, I like to keep them around when I work
with E4X, as .toString() doesn't give the whole truth for cases like
<a href="...">oops</a>.toString(), but it matters less here.)

>> There is some old discussion on the topic on greasemonkey-dev
>> if you search for GM_headers.
>
> BTW, +1 on GM_headers, AND +1 on @xpath, too, FWIW from a person with
> zero dev experience here; I know @xpath would've simplified the coding
> of my (one and only public) script... :-)

Glad to hear it; the whole point with @xpath was usability for
everybody that *hasn't* spent the last ten years getting expert at
spanking the DOM to go their errands in short work. I have failed to
convince Anthony and Aaron it's a good enough idea, though.

Assuming I'm the only one working on GM_headers, I'm probably the
largest obstacle in front of that at the moment, as I haven't come up
with code good enough for it yet to be content myself -- there isn't
any oppossition to it, from what I recall. :-)

Maybe we should set up something akin to
http://userscripts.uservoice.com/ to probe the popularity of proposed
features and changes, and get some more influx of ideas in an
overviewable browsey manner for things like that. Won't likely
overrule any vetoes, but at least I would really love a more
approachable open air place than greasemonkey-dev and Trac.

(Would probably also be helpful to give myself a swift kick in the bum
to actually sweat out the API for it, implement and be done with it --
knowing that tons of people want the same thing you do is powerful
encouragement to work on it.)

Odpovědět všem
Odpověď autorovi
Přeposlat
0 nových zpráv