This repository has been archived by the owner on Oct 22, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b773bb9
commit ee7ba54
Showing
6 changed files
with
210 additions
and
13 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1"> | ||
<style> | ||
* { | ||
box-sizing: border-box; | ||
margin: 0; | ||
padding: 0; | ||
} | ||
body { | ||
font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif; | ||
} | ||
pre { | ||
font-family: Menlo,Monaco,Consolas,"Courier New",monospace; | ||
font-size: 14px; | ||
overflow: auto; | ||
margin: 0; | ||
padding: 8px; | ||
background-color: #ECEFF1; | ||
overflow-x: auto; | ||
min-height: 32px; | ||
} | ||
-webkit-scrollbar, pre::-webkit-scrollbar { | ||
height: 8px; | ||
width: 4px; | ||
} | ||
pre::-webkit-scrollbar-thumb { | ||
background: rgba(0,0,0,.26); | ||
} | ||
a { | ||
color: #37474f; | ||
text-decoration: none; | ||
} | ||
a:hover { | ||
text-decoration: underline; | ||
} | ||
.error { | ||
color: red; | ||
} | ||
.input-container { | ||
display: flex; | ||
} | ||
.input-container .responsive { | ||
flex: 1; | ||
} | ||
.input-container .fixed { | ||
flex: 0; | ||
margin: 0 0 0 16px; | ||
} | ||
.input-container div input { | ||
font-size: 14px; | ||
padding: 6px; | ||
width: 100%; | ||
} | ||
.input-container div button { | ||
font-size: 14px; | ||
padding: 8px 16px; | ||
white-space: nowrap; | ||
} | ||
.result-container { | ||
padding: 16px 0 0 0; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<section class="input-container"> | ||
<div class="responsive"> | ||
<input id="input" type="text" /> | ||
</div> | ||
<div class="fixed"> | ||
<button id="execute">Convert</button> | ||
</div> | ||
</section> | ||
<section class="result-container"> | ||
<pre id="result"></pre> | ||
</section> | ||
<script src="https://cdn.ampproject.org/viewer/google/amp_url.js"></script> | ||
<script src="/scripts/amp_url_converter.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
const DEFAULT_URL = "https://www.example.com/amp?param=value"; | ||
const AMP_CACHE_PREFIX = "https://cdn.ampproject.org/c/"; | ||
|
||
class AmpUrlConverter { | ||
|
||
constructor(root, ampUrlFactory) { | ||
this.inputView = root.getElementById('input'); | ||
this.resultView = root.getElementById('result'); | ||
this.ampUrlFactory = ampUrlFactory; | ||
root.getElementById('execute') | ||
.addEventListener("click", this.onClick.bind(this)); | ||
} | ||
|
||
setInput(urlString) { | ||
this.inputView.value = urlString; | ||
this.convert(urlString); | ||
} | ||
|
||
convert(urlString) { | ||
urlString = urlString.trim(); | ||
if (!urlString) { | ||
this.showError('Empty input'); | ||
return; | ||
} | ||
try { | ||
const ampUrl = this.ampUrlFactory.createAmpUrl(urlString); | ||
const proxyUrl = ampUrl.getProxyUrl(); | ||
this.showResult('<a href="' + proxyUrl + '" target="_parent">' + proxyUrl + '</a>'); | ||
}catch(e) { | ||
this.showError('Invalid URL'); | ||
} | ||
} | ||
|
||
showError(message) { | ||
this.resultView.className = 'error'; | ||
this.resultView.innerHTML = message; | ||
} | ||
|
||
showResult(result) { | ||
this.resultView.className = ''; | ||
this.resultView.innerHTML = result; | ||
} | ||
|
||
onClick() { | ||
this.convert(this.inputView.value); | ||
} | ||
|
||
} | ||
|
||
function getParameterByName(name, defaultValue) { | ||
const url = window.location.href; | ||
name = name.replace(/[\[\]]/g, "\\$&"); | ||
const regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), | ||
results = regex.exec(url); | ||
if (!results) { return defaultValue; } | ||
if (!results[2]) { return defaultValue; } | ||
return decodeURIComponent(results[2].replace(/\+/g, " ")); | ||
} | ||
|
||
const proxyUrlPrefix = 'https://cdn.ampproject.org'; | ||
const javascriptVersion = '5'; | ||
const useCurlsEncoding = true; | ||
const ampUrlFactory = | ||
new AmpUrlFactory(proxyUrlPrefix, javascriptVersion, useCurlsEncoding); | ||
|
||
const converter = new AmpUrlConverter(document, ampUrlFactory); | ||
const initialUrl = getParameterByName('url', 'https://www.example.com/amp?param=1'); | ||
|
||
converter.setInput(initialUrl); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters