Skip to content

Commit

Permalink
✨Use WebAssembly validator for chrome extension (#34502)
Browse files Browse the repository at this point in the history
* Use WebAssembly validator for chrome extension

* Use es5 for popup.html

* Init wasm before polymer code
  • Loading branch information
antiphoton authored May 24, 2021
1 parent 048f036 commit 40a8d20
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
2 changes: 1 addition & 1 deletion validator/js/chromeextension/background.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.ampproject.org/v0/validator.js"></script>
<script src="https://cdn.ampproject.org/v0/validator_wasm.js"></script>
<script src="background.js"></script>
</head>
<body></body>
Expand Down
3 changes: 2 additions & 1 deletion validator/js/chromeextension/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,13 @@ function validateUrlFromTab(tab, userAgent) {
// Add the temporary header to the request
xhr.setRequestHeader(globals.userAgentHeader, userAgent);

xhr.onreadystatechange = function() {
xhr.onreadystatechange = async function() {
if (xhr.readyState === 4) {
// The request is complete; remove our temporary listener.
chrome.webRequest.onBeforeSendHeaders.removeListener(
updateSendHeadersUserAgent);
const doc = xhr.responseText;
await amp.validator.init();
const validationResult = amp.validator.validateString(doc);
window.sessionStorage.setItem(url, JSON.stringify(validationResult));
if (validationResult.status == 'PASS') {
Expand Down
6 changes: 3 additions & 3 deletions validator/js/chromeextension/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "__MSG_extensionName__",
"version": "1.1.8",
"version": "1.2.0",
"default_locale": "en",
"description": "__MSG_extensionDescription__",
"icons": {
Expand All @@ -25,8 +25,8 @@
"run_at": "document_start"
}
],
"content_security_policy": "script-src 'self' https://cdn.ampproject.org; object-src 'self'",
"homepage_url": "https://www.ampproject.org",
"content_security_policy": "script-src 'self' https://cdn.ampproject.org 'wasm-eval'; object-src 'self'",
"homepage_url": "https://amp.dev",
"permissions": [
"tabs",
"webRequest",
Expand Down
46 changes: 25 additions & 21 deletions validator/js/chromeextension/popup-validator.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<script src="./bower_components/webcomponentsjs/CustomElements.min.js"></script>
<link href="polymer.html" rel="import">
<link href="polymer-extension-toolbar.html" rel="import">
<script src="https://cdn.ampproject.org/v0/validator.js"></script>
<script src="https://cdn.ampproject.org/v0/validator_wasm.js"></script>
<style is="custom-style">
body {
background-color: #fff;
Expand Down Expand Up @@ -126,25 +126,27 @@
</paper-listbox>
</template>
<script>
Polymer({
is: 'results-page',
getIssue: function(item) {
return amp.validator.renderErrorMessage(item);
},
hasResults: function(results) {
return results.length > 0;
},
properties: {
emptyResultsText: {
type: String,
value: chrome.i18n.getMessage("emptyResultsText"),
amp.validator.init().then(function() {
Polymer({
is: 'results-page',
getIssue: function(item) {
return amp.validator.renderErrorMessage(item);
},
results: {
type: Array,
value: function() { return []; },
notify: true,
hasResults: function(results) {
return results.length > 0;
},
},
properties: {
emptyResultsText: {
type: String,
value: chrome.i18n.getMessage("emptyResultsText"),
},
results: {
type: Array,
value: function() { return []; },
notify: true,
},
},
});
});
</script>
</dom-module>
Expand Down Expand Up @@ -224,9 +226,11 @@
xhr.open('GET', url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
validationResult =
amp.validator.validateString(xhr.responseText);
processResult(validationResult, toolbar, content, url);
amp.validator.init().then(function() {
validationResult =
amp.validator.validateString(xhr.responseText);
processResult(validationResult, toolbar, content, url);
});
}
};
xhr.send();
Expand Down

0 comments on commit 40a8d20

Please sign in to comment.