-
Notifications
You must be signed in to change notification settings - Fork 512
/
Copy pathWebExtExamples.ejs
114 lines (96 loc) · 3.5 KB
/
WebExtExamples.ejs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<%
/*
The "examples.json" file from the https://github.com/mdn/webextensions-examples
repository contains information about the examples living in that repository.
This macro must be embedded in a WebExtensions API page, either of:
* a "module" page (for example: "/en-US/Add-ons/WebExtensions/API/alarms" or "/en-US/Add-ons/WebExtensions/API/tabs")
* an "API" page (for example: "/en-US/Add-ons/WebExtensions/API/alarms/create" or "/en-US/Add-ons/WebExtensions/API/tabs/executeScript")
If it's embedded in a module page, it fetches all the examples that use any
of the APIs in the given module, and outputs a list of links to those examples.
If it's embedded in an API page, it fetches all the examples that use the
given API, and outputs a list of links to those examples.
It prepends this list with a header. By default the header is <h3>.
The macro takes one optional argument, which is the name of the tag to use
for the header. So if you want the header to be an <h2>, call it with "h2".
*/
const allExamples = await MDN.fetchWebExtExamples();
const examplesBaseURL = "https://github.com/mdn/webextensions-examples/tree/main/";
const desiredHeading = $0 || "h3";
const s_example_addons = mdn.localString({
"de": "Beispiel-Erweiterungen",
"en-US": "Example extensions",
"zh-CN": "示例扩展",
"zh-TW": "範例擴充套件",
});
/*
The `module` argument is the name of a JavaScript API module, like "alarms"
or "tabs".
This function returns an array of JSON objects representing examples which use
any of the APIs under the given module.
*/
function getExamplesForModule(module) {
let examplesForModule = [];
for (let example of allExamples) {
for (let jsAPI of example.javascript_apis) {
jsAPI = jsAPI.split(".")[0];
if (jsAPI === module) {
examplesForModule.push(example);
break;
}
}
}
return examplesForModule;
}
/*
The `module` argument is the name of a JavaScript API module, like "alarms"
or "tabs".
The `api` argument is the name of a JavaScript API in that module, like "create"
or "executeScript".
This function returns an array of JSON objects representing examples which the
given API, like "alarms.create" or "tabs.executeScript".
*/
function getExamplesForAPI(module, api) {
let examplesForAPI = [];
let moduleAndAPI = `${module}.${api}`;
for (let example of allExamples) {
for (let jsAPI of example.javascript_apis) {
if (moduleAndAPI === jsAPI) {
examplesForAPI.push(example);
break;
}
}
}
return examplesForAPI;
}
/*
Given an array of JSON objects representing example add-ons,
this function outputs:
* a header
* a list of links to the example source in GitHub
*/
function writeExampleLinks(examples) {
let output = "";
if (examples.length > 0) {
output = `<${desiredHeading}>${s_example_addons}</${desiredHeading}>`;
output += "<ul>";
for (let example of examples) {
output += `<li><a href="${examplesBaseURL}${example.name}">${example.name}</a></li>`;
}
output += "</ul>";
}
return output;
}
let output = "";
let pieces = env.slug.split("/");
let leaf = pieces[pieces.length-1];
let parent = pieces[pieces.length-2];
// If the immediate parent of this page is named "API", assume we are in a module page, like "alarms" or "tabs".
if (parent == "API") {
output = writeExampleLinks(getExamplesForModule(leaf));
}
// Otherwise, assume we are in an API page, like "alarms.create" or "tabs.executeScript".
else {
output = writeExampleLinks(getExamplesForAPI(parent, leaf));
}
%>
<%- output %>