90

I'm learning how to use Greasemonkey, and was wondering what the @namespace metadata id is for.

Does it have to be a web address? Or can it be a folder/directory on my computer?

Does it even need to be filled in?

andrybak
  • 2,129
  • 2
  • 20
  • 40
Nope
  • 34,682
  • 42
  • 94
  • 119

4 Answers4

88

A namespace is used to avoid naming collisions. If you called your script foobar and someone else did as well, then central repositories would have a hard time telling them apart.

Therefore you should provide some URL that you control (i.e. you own it or can administrate it) that basically means "everything with that URL is by me". Now those central repositories can distinguish between foobar from http://somesite.com/ and foobar from http://anothersite.com.

It's not necessary for basic operation, but strongly suggested if you want to share your scripts.

Keep in mind that mailto:someone@example.com is also a valid URL and might be a possible option when you don't own or control a domain of your own.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • 6
    @GrantThomas: according to [this Wiki](http://wiki.greasespot.net/Metadata_Block#.40namespace): "While the namespace is non-semantic, a URL is often used." So no, it need not be an URL, but an URL you "own" is a simple way to guarantee that the value is unique. – Joachim Sauer Sep 08 '13 at 07:40
  • Thanks. Just I'm always buying domains, using them a while, then letting them go. I even have the same problem on deciding non-URL namespacing, such as in .NET APIs - but that's a different issue and not half as problematic of making up a URL you don't own but think might just be 'safe'. Thanks again. – Grant Thomas Sep 08 '13 at 16:41
  • 3
    @GrantThomas: you could use your email address (or even a `mailto:` URI, if you want to make it an URL), for example. – Joachim Sauer Sep 08 '13 at 16:53
  • That actually seems to be a sound idea. – Grant Thomas Sep 08 '13 at 16:57
  • is it okay to update the script with new namespace? I have a userscript shared publicly in greasyfork and I wrote a random URL as the namespace. – Christhofer Natalius Jul 21 '16 at 08:13
  • @Christ: I honestly don't know. It's possible that upgrade mechanisms break if you do that, but it depends on the specific site. – Joachim Sauer Jul 21 '16 at 10:33
  • 1
    @Joachim Sauer Greasyfork warned me when I tried to save the script with new namespace, so I canceled it. Ok then I'll just leave it as it is. Thanks for the answer. – Christhofer Natalius Jul 21 '16 at 12:36
  • I guess this answer probably applies for TamperMonkey too? – Adam Spiers Jun 27 '21 at 20:57
  • 1
    @AdamSpiers: since TamperMonkey can install the same scripts, I assume so. – Joachim Sauer Jun 28 '21 at 07:35
22

One place you can see the practical effect of namespaces is in storing preferences. Nampsaces are used to uniquely identify scripts for any script-specific stored preferences.

For example, if you have a script like this:

// ==UserScript==
// @name            Script Name
// @namespace       http://example.com
// @include         *
// ==/UserScript==


GM_setValue("key", "value");

That would be stored in your preferences (accessible in prefs.js, and about:config) like so:

greasemonkey.scriptvals.http://example.com/Script Name.key

Note the format: greasemonkey.scriptvals . namespace . scriptname . key/variablename

Athena
  • 3,130
  • 1
  • 19
  • 17
10

In general, a namespace is an abstract container providing context for the items (names, or technical terms, or words) it holds and allowing disambiguation of items having the same name (residing in different namespaces).

Source: Namespace - Wikipedia

And more specific:

This is a URL, and Greasemonkey uses it to distinguish user scripts that have the same name but are written by different authors. If you have a domain name, you can use it (or a subdirectory) as your namespace. Otherwise you can use a tag: URI.

@namespace is optional. If present, it may appear only once. If not present, it defaults to the domain from which the user downloaded the user script.

Source: Dive Into Greasemonkey - Metadata

Community
  • 1
  • 1
Tamara Wijsman
  • 12,198
  • 8
  • 53
  • 82
1

Namespace can be a URL, but not only. You can use as namespace some words as your username or real name.

XXN
  • 128
  • 9