@@ -49,7 +49,7 @@ Can be used as follows in the declaration:
49
49
}
50
50
```
51
51
52
- Example:
52
+ #### Example
53
53
54
54
``` js
55
55
export function convertTimeAgoToDate (document ) {
@@ -114,13 +114,15 @@ Can be used as follows in the declaration:
114
114
}
115
115
```
116
116
117
- Example:
117
+ #### Example 1
118
118
119
119
``` js
120
- export function removeLinksWithText (document , text ) {
120
+ export function removeLinksWithText (document , textArray ) {
121
121
const links = document .querySelectorAll (' a' );
122
+ const textsToRemove = Array .isArray (textArray) ? textArray : [textArray];
123
+
122
124
links .forEach (link => {
123
- if (link .textContent .trim () === text ) {
125
+ if (textsToRemove . includes ( link .textContent .trim ()) ) {
124
126
link .remove ();
125
127
}
126
128
});
@@ -135,8 +137,7 @@ export function removeLinksWithText(document, text) {
135
137
"fetch" : " https://example.com/privacy" ,
136
138
"select" : " .content" ,
137
139
"filter" : [
138
- { "removeLinksWithText" : " Return to previous section" }
139
- { "removeLinksWithText" : " Go to next section" }
140
+ { "removeLinksWithText" : [" Return to previous section" , " Go to next section" ] }
140
141
]
141
142
}
142
143
}
@@ -160,3 +161,51 @@ Result:
160
161
<p>...</p>
161
162
</div>
162
163
```
164
+
165
+ #### Example 2
166
+
167
+ ``` js
168
+ import fetch from ' isomorphic-fetch' ;
169
+
170
+ export async function convertImagesToBase64 (document , selector , documentDeclaration ) {
171
+ const images = Array .from (document .querySelectorAll (` selector` ));
172
+
173
+ return Promise .all (images .map (async ({ src }, index ) => {
174
+ if (src .startsWith (' data:' )) {
175
+ return ; // Already a data-URI, skip
176
+ }
177
+
178
+ const imageUrl = new URL (src, documentDeclaration .fetch ).href ; // Ensure url is absolute
179
+ const response = await fetch (imageUrl);
180
+ const mimeType = response .headers .get (' content-type' );
181
+ const content = await response .arrayBuffer ();
182
+
183
+ const base64Content = btoa (String .fromCharCode (... new Uint8Array (content)));
184
+
185
+ images[index].src = ` data:${ mimeType} ;base64,${ base64Content} ` ;
186
+ }));
187
+
188
+ }
189
+ ```
190
+
191
+ ``` json
192
+ {
193
+ "name" : " MyService" ,
194
+ "terms" : {
195
+ "Privacy Policy" : {
196
+ "fetch" : " https://example.com/privacy" ,
197
+ "select" : " .content" ,
198
+ "filter" : [
199
+ { "convertImagesToBase64" : " .meaningful-illustration" }
200
+ ]
201
+ }
202
+ }
203
+ }
204
+ ```
205
+
206
+ Result:
207
+
208
+ ``` diff
209
+ - <img src="https://example.com/image.png" class="meaningful-illustration">
210
+ + <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA..." class="meaningful-illustration">
211
+ ```
0 commit comments