What is the Greasemonkey namespace needed for?

MetadataNamespacesGreasemonkey

Metadata Problem Overview


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?

Metadata Solutions


Solution 1 - Metadata

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:[email protected] is also a valid URL and might be a possible option when you don't own or control a domain of your own.

Solution 2 - Metadata

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

Solution 3 - Metadata

> 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

Solution 4 - Metadata

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNopeView Question on Stackoverflow
Solution 1 - MetadataJoachim SauerView Answer on Stackoverflow
Solution 2 - MetadataAthenaView Answer on Stackoverflow
Solution 3 - MetadataTamara WijsmanView Answer on Stackoverflow
Solution 4 - MetadataXXNView Answer on Stackoverflow