Skip to content

Instantly share code, notes, and snippets.

@Rob--W
Created July 27, 2018 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rob--W/0396c2f229e15eeedc9f776fb7c91dc8 to your computer and use it in GitHub Desktop.
Save Rob--W/0396c2f229e15eeedc9f776fb7c91dc8 to your computer and use it in GitHub Desktop.
Usage of "contexts" in the contextMenus / menus API in public add-ons at addons.mozilla.org
$ ### This is a rough estimate of the values of the "contexts" array in extensions,
$ ### presumably used as values in the contextMenus/menus.create/update API.
$ ### First, we check whether the number of reported occurrences is significant,
$ ### by checking the number of "contexts:" strings that are not followed by an array.
$ ### As the first checks show, only 10% of the matches are not arrays,
$ ### (so they are variables, expressions or not part of the menus API).
$ ### The other 90% are arrays, and significant.
$ ### LIMITATIONS
$ ### This analysis only accounts for object literals, and not for dynamic assignments,
$ ### such as foo.contexts = [...].
$ ### METHODOLOGY
$ ### First, download all public extensions from AMO, using https://github.com/cr/webextaware
$ ### At the time of analysis (27 july 2018), there are 15.2k extensions.
$ ### I selected all extensions whose manifest.json contained "menus" or "contextMenus" (quotes included).
$ ### The directory structure generated by "webextaware unzip" is number/hash/hash/{manifest.json,...}
$ ### The number of unique extenson listings that use the contextMenus/menus APIs
$ ls -ld * | wc -l
2096
$ ### The number of extensions (including multiple versions) that use the APIs
$ ls -ld */* | wc -l
2992
$ ### The number of extensions where "contexts:" is not followed by an array.
$ grep -r --include='*.js' -oPaz 'contexts["'"'"']?\s*:\s*\K[^\[\s]' -l | grep -oP '^[0-9]+\/[a-f0-9]+' | sort | uniq | wc -l
230
$ ### The number of files where "contexts:" is not followed by an array
$ grep -r --include='*.js' -oPaz 'contexts["'"'"']?\s*:\s*\K[^\[\s]' -l | wc -l
257
$ ### The number of occurrence of "contexts:" not followed by an array
$ grep -r --include='*.js' -oPaz 'contexts["'"'"']?\s*:\s*\K[^\[\s]' | tr '\0' '\n' | wc -l
836
$ ### The number of files where "contexts:" is followed by an array
$ grep -r --include='*.js' -oPaz 'contexts["'"'"']?\s*:\s*\K\[\s*[^\]]+\]' -l | wc -l
2463
$ ### The number of extensions where "contexts:" is followed by an array
$ grep -r --include='*.js' -oPaz 'contexts["'"'"']?\s*:\s*\K\[\s*[^\]]+\]' -l | grep -oP '^[0-9]+\/[a-f0-9]+' | sort | uniq | wc -l
2349
$ ### The number of occurences of "contexts:" followed by an array
$ grep -r --include='*.js' -oPaz 'contexts["'"'"']?\s*:\s*\K\[\s*[^\]]+\]' | tr '\n' ' ' | sed 's/\x00//g; s/\s//g; s/[0-9]\+\/[^:]\+:/\x00/g; s/^\x00//; s/\x00/\n/g; s/"/'"'"'/g' | wc -l
7406
$ ### The exact values of the arrays after "contexts:" (without whitespace)
$ grep -r --include='*.js' -oPaz 'contexts["'"'"']?\s*:\s*\K\[\s*[^\]]+\]' | tr '\n' ' ' | sed 's/\x00//g; s/\s//g; s/[0-9]\+\/[^:]\+:/\x00/g; s/^\x00//; s/\x00/\n/g; s/"/'"'"'/g' | sort | uniq -c | sort -rn
1652 ['selection']
1607 ['all']
607 ['link']
534 ['browser_action']
407 ['image']
406 ['page']
253 ['tab']
240 ['editable']
162 [context1,'browser_action']
139 [context]
60 ['tools_menu']
45 ['page','tab']
44 ['selection','editable']
37 ['selection','link']
36 ['page','frame','selection','link','editable','image','video','audio']
35 ['page','link']
33 [_0x1b65('0x31'),_0x1b65('0x32')]
32 ['browser_action','page_action']
30 ['link','page','selection']
30 [_0x49d0[101]
28 ['frame']
25 ['image','video','audio']
25 [_0xf372('0x6')]
23 ['selection','page']
23 ['link','image']
22 ['link','selection']
21 ['video']
21 ['page_action']
21 [`page`]
21 ['all','tools_menu']
19 ['video','link']
18 ['tab','all']
18 ['page','frame']
18 ['audio','video','link']
18 ['audio','link']
18 ['all','tab']
16 ['link','page']
15 [e]
15 ['bookmark']
14 ['video','audio']
14 ['page','selection']
14 ['link','audio','video']
13 ['page_action','browser_action']
12 ['video','tab','page']
12 [context==='reset'?'image':context]
12 [context0,'browser_action']
12 [browser.menus.ContextType.ALL,browser.menus.ContextType.TAB]
12 ['all','page','frame','selection','link','editable','image','video','audio','launcher','browser_action','page_action']
11 ['page','editable','frame']
11 ['editable','password']
11 ['audio']
10 ['password']
10 ['page','selection','link']
10 ['page','selection','image','link']
10 ['editable',_0x1b65('0x32')]
10 [_0x1b65('0x31'),'password']
9 ['page','selection','link','editable','image','video','audio']
9 ['page','link','image']
9 ['page','editable']
9 [c]
9 [browser.contextMenus.ContextType.EDITABLE,browser.contextMenus.ContextType.FRAME,browser.contextMenus.ContextType.IMAGE,browser.contextMenus.ContextType.LINK,browser.contextMenus.ContextType.PAGE,browser.contextMenus.ContextType.SELECTION]
9 [browser.contextMenus.ContextType.EDITABLE,browser.contextMenus.ContextType.FRAME,browser.contextMenus.ContextType.IMAGE,browser.contextMenus.ContextType.LINK,browser.contextMenus.ContextType.PAGE,browser.contextMenus.ContextType.PASSWORD,browser.contextMenus.ContextType.SELECTION]
9 ['browser_action','page','tab']
9 [`browser_action`]
9 [_0xcd9e[224]
8 ['selection','link','image']
8 ['page','browser_action']
7 [type]
7 [t]
7 ['link','video','audio']
7 ['image','video']
7 ['image','link']
7 [_0xa8c4bf]
6 ['page','selection','image','link','all']
6 ['page','frame','selection','editable','image','video','audio']
6 [n]
6 ['link','video']
6 ['link','selection','editable']
6 ['image','audio','video']
6 [document,...]
5 ['selection','link','editable','image']
5 ['page','frame','selection','link','video','audio','image']
5 [browser.contextMenus.ContextType.TAB]
5 ['browser_action','page']
5 ['audio','video']
4 ['selection','link','page']
4 ['selection','link','editable']
4 ['selection','image']
4 ['page','selection','image']
4 ['page','link','selection','image']
4 ['page','image','link']
4 ['page','frame','image']
4 ['image','page']
4 ['editable','page']
4 [ctx]
4 ['audio','editable','frame','image','page','password','selection','video']
4 [_0x523f09[_0xf372('0x2b3')]
3 ['\x73\x65\x6c\x65\x63\x74\x69\x6f\x6e']
3 ['tab','page']
3 ['selection','video','frame']
3 ['selection','image','video','audio','link','page']
3 ['selection','image','link']
3 [prefs.video?'video':'',prefs.audio?'audio':'',prefs.link?'link':'']
3 ['page','tab','selection']
3 ['page'//'selection',//'link',//'editable',//'image',//'video',//'audio']
3 ['page','image','selection','link']
3 ['page','image','selection']
3 ['page','image','link','selection']
3 ['page','image']
3 ['page','frame','selection','editable','video']
3 ['page','frame','link','image','video','audio']
3 [menu[4]
3 ['link','selection','frame','page']
3 ['link','editable','selection']
3 ['link','audio','video','image']
3 [`link`]
3 ['+contexts.join(',')+']
3 ['all'//'image']
3 [_0xf372('0x34'),_0xf372('0xb'),_0xf372('0xc')]
3 [_0xcd9e[160]
3 [_0x4f85b9[_0xf372('0x61')]
3 [_0x421c[152]
3 [_0x18e0('0x6')]
2 ['video','audio','image']
2 ['tools_menu','browser_action']
2 [sContext]
2 ['page','selection','link','image','video','audio']
2 ['page','frame','selection','link','editable','image','video','audio','page_action']
2 ['page','frame','selection','link','editable']
2 ['page','frame','selection','editable']
2 ['page','editable','frame','link','image','video']
2 ['page','browser_action','password']
2 ['page','browser_action','editable']
2 ['link','image','video','audio','selection']
2 ['link','audio']
2 ['editable','selection']
2 ['browser_action','page','frame']
2 [a[_0x3e1e('0x3a')]
2 [a[_0x3e1e('0x16')]
2 [_0xf372('0x38')]
2 [_0x3bd84e]
2 [_0x25e5e4[_0xf372('0x195')]
2 [_0x18e0('0x3')]
1 ['\x73\x65\x6c\x65\x63\x74\x69\x6f\x6e',_0xf372('0x34'),_0xf372('0xb'),_0xf372('0xc')]
1 ['video','image']
1 [TYPES_CONTEXT.LINK]
1 [service.contextmenu]
1 ['selection','image','video','audio']
1 [prefs.video?'video':'',prefs.audio?'audio':'']
1 ['page','tools_menu']
1 ['PAGE','TABS']
1 ['page','selection','link','image','video']
1 ['page','selection','link','browser_action','page_action']
1 ['page','selection','editable','link','tab']
1 ['page','selection','editable','image','video','audio']
1 ['page','password','editable']
1 ['page','link','selection']
1 ['page','image','frame','editable','video','selection']
1 ['page','frame','selection','link','editable','image']
1 ['page','frame','selection','editable','video','audio']
1 ['page','frame','selection']
1 ['page','frame','link']
1 ['page',//firefox-only:'tab',]
1 ['page','editable','password']
1 ['page','editable','link']
1 ['page','browser_action','link']
1 [onwhat]
1 [_oContexts[_0x1882b4]
1 ['link','selection','image','video','audio']
1 ['link','page','tab']
1 ['link','browser_action']
1 ['link','audio','video','selection']
1 [(IS_BUTTON_ON_TOOLBAR?'browser_action':'page_action')]
1 [$intern_57,$intern_58,$intern_59,$intern_60,$intern_61,$intern_62,$intern_63]
1 ['frame','page','selection','video']
1 [e['\x68\x41\x6f\x54\x73']
1 [`editable`]
1 [e.ContextType.selection,e.ContextType.link]
1 [contexts[j]
1 [contexts[i]
1 ['audio','image','link','page','tab','video']
1 ['audio','editable','frame','image','link','page','page_action','password','selection','tab','video']
1 ['all','tab','tools_menu']
1 ['all','selection']
1 ['all','page_action']
1 ['all',]
1 [a[_0x61c7('0x53')]
1 [a[_0x4d17('0x41')]
1 [a[_0x0208('0x1d')]
1 [_0xf372('0x6'),'\x70\x61\x67\x65',_0xf372('0xb'),_0xf372('0xc')]
1 [_0xf372('0x6'),_0xf372('0x34'),_0xf372('0xb'),'\x74\x61\x62']
1 [_0xf372('0x6'),_0xf372('0x34'),_0x4521db[_0xf372('0x9e')]
1 [_0xf372('0x6'),_0x523f09[_0xf372('0x2b2')]
1 [_0xf372('0x34'),_0xf372('0xb'),'\x74\x61\x62']
1 [_0x630c('0x18')]
1 [_0x5c98('0x34')]
1 [_0x5288e5['\x6e\x41\x45\x50\x59']
1 [_0x5288e5[_0xf372('0x220')]
1 [_0x523f09[_0xf372('0x2b2')]
1 [_0x4d2555]
1 [_0x3d4782]
1 [_0x3c2ed8[_0xf372('0x24a')]
1 [_0x38b6('0x2')]
1 [_0x1b65('0x2d')]
1 [_0x166e95]
1 [_0x12bc76[_0xf372('0x1d7')]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment