我有这个脚本
function showLucky(root){
var feed = root.feed;
var entries = feed.entry || [];
var entry = feed.entry[0];
for (var j = 0; j < entry.link.length; ++j) {
if (entry.link[j].rel == "alternate") {
window.location = entry.link[j].href;
}
}
}
function fetchLuck(luck){
script = document.createElement('script');
script.src = '/feeds/posts/summary/-/1 to 10?start-index='+luck+'&max-results=1&alt=json-in-script&callback=showLucky';
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);
}
function readLucky(root){
var feed = root.feed;
var total = parseInt(feed.openSearch$totalResults.$t,10);
var luckyNumber = Math.floor(Math.random()*total);
luckyNumber++;
fetchLuck(luckyNumber);
}
function feelingLucky(){
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = '/feeds/posts/summary/-/1 to 10?max-results=0&alt=json-in-script&callback=readLucky';
document.getElementsByTagName('head')[0].appendChild(script);
}
//]]>
.btn_tc {
display: inline-block;
margin: 0.3em;
padding: 15px 35px;
overflow: hidden;
position: relative;
text-decoration: none;
text-transform: uppercase;
border-radius: 3px;
-webkit-transition: 0.3s;
-moz-transition: 0.3s;
-ms-transition: 0.3s;
-o-transition: 0.3s;
transition: 0.3s;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
border: none;
font-size: 15px;
text-align: center;
}
.btn_tc:hover {
box-shadow: 1px 6px 15px rgba(0, 0, 0, 0.5);
}
.green_tc {
background-color: #4CAF50;
color: white!important;
}
<a class="btn_tc green_tc" href="#random" onclick="feelingLucky()" title="Random Post">Continue</a>
哪个可以在NORMAL网站上正常工作。从特定标签(标签)加载随机帖子。
我正在尝试通过以下方法将此脚本包含到AMP网站:
<script async='async' custom-element='amp-script' src='https://cdn.ampproject.org/v0/amp-script-0.1.js'/>
<amp-script layout="container" src="https://mysite/random.js"></amp-script>
<a class="btn_tc green_tc" href="#random" title="Random Post">Continue</a>
显然无法正常工作,我知道我缺少了一些东西,但我真的不知道该集中精力在AMP版本上工作。