Skip to content

Commit 40a8d20

Browse files
authored
✨Use WebAssembly validator for chrome extension (#34502)
* Use WebAssembly validator for chrome extension * Use es5 for popup.html * Init wasm before polymer code
1 parent 048f036 commit 40a8d20

File tree

4 files changed

+31
-26
lines changed

4 files changed

+31
-26
lines changed

validator/js/chromeextension/background.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<html>
1818
<head>
1919
<meta charset="utf-8">
20-
<script src="https://cdn.ampproject.org/v0/validator.js"></script>
20+
<script src="https://cdn.ampproject.org/v0/validator_wasm.js"></script>
2121
<script src="background.js"></script>
2222
</head>
2323
<body></body>

validator/js/chromeextension/background.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,13 @@ function validateUrlFromTab(tab, userAgent) {
315315
// Add the temporary header to the request
316316
xhr.setRequestHeader(globals.userAgentHeader, userAgent);
317317

318-
xhr.onreadystatechange = function() {
318+
xhr.onreadystatechange = async function() {
319319
if (xhr.readyState === 4) {
320320
// The request is complete; remove our temporary listener.
321321
chrome.webRequest.onBeforeSendHeaders.removeListener(
322322
updateSendHeadersUserAgent);
323323
const doc = xhr.responseText;
324+
await amp.validator.init();
324325
const validationResult = amp.validator.validateString(doc);
325326
window.sessionStorage.setItem(url, JSON.stringify(validationResult));
326327
if (validationResult.status == 'PASS') {

validator/js/chromeextension/manifest.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"manifest_version": 2,
33
"name": "__MSG_extensionName__",
4-
"version": "1.1.8",
4+
"version": "1.2.0",
55
"default_locale": "en",
66
"description": "__MSG_extensionDescription__",
77
"icons": {
@@ -25,8 +25,8 @@
2525
"run_at": "document_start"
2626
}
2727
],
28-
"content_security_policy": "script-src 'self' https://cdn.ampproject.org; object-src 'self'",
29-
"homepage_url": "https://www.ampproject.org",
28+
"content_security_policy": "script-src 'self' https://cdn.ampproject.org 'wasm-eval'; object-src 'self'",
29+
"homepage_url": "https://amp.dev",
3030
"permissions": [
3131
"tabs",
3232
"webRequest",

validator/js/chromeextension/popup-validator.html

+25-21
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<script src="./bower_components/webcomponentsjs/CustomElements.min.js"></script>
2020
<link href="polymer.html" rel="import">
2121
<link href="polymer-extension-toolbar.html" rel="import">
22-
<script src="https://cdn.ampproject.org/v0/validator.js"></script>
22+
<script src="https://cdn.ampproject.org/v0/validator_wasm.js"></script>
2323
<style is="custom-style">
2424
body {
2525
background-color: #fff;
@@ -126,25 +126,27 @@
126126
</paper-listbox>
127127
</template>
128128
<script>
129-
Polymer({
130-
is: 'results-page',
131-
getIssue: function(item) {
132-
return amp.validator.renderErrorMessage(item);
133-
},
134-
hasResults: function(results) {
135-
return results.length > 0;
136-
},
137-
properties: {
138-
emptyResultsText: {
139-
type: String,
140-
value: chrome.i18n.getMessage("emptyResultsText"),
129+
amp.validator.init().then(function() {
130+
Polymer({
131+
is: 'results-page',
132+
getIssue: function(item) {
133+
return amp.validator.renderErrorMessage(item);
141134
},
142-
results: {
143-
type: Array,
144-
value: function() { return []; },
145-
notify: true,
135+
hasResults: function(results) {
136+
return results.length > 0;
146137
},
147-
},
138+
properties: {
139+
emptyResultsText: {
140+
type: String,
141+
value: chrome.i18n.getMessage("emptyResultsText"),
142+
},
143+
results: {
144+
type: Array,
145+
value: function() { return []; },
146+
notify: true,
147+
},
148+
},
149+
});
148150
});
149151
</script>
150152
</dom-module>
@@ -224,9 +226,11 @@
224226
xhr.open('GET', url, true);
225227
xhr.onreadystatechange = function() {
226228
if (xhr.readyState === 4) {
227-
validationResult =
228-
amp.validator.validateString(xhr.responseText);
229-
processResult(validationResult, toolbar, content, url);
229+
amp.validator.init().then(function() {
230+
validationResult =
231+
amp.validator.validateString(xhr.responseText);
232+
processResult(validationResult, toolbar, content, url);
233+
});
230234
}
231235
};
232236
xhr.send();

0 commit comments

Comments
 (0)