我做了一个 chrome 扩展。它非常简单。有一系列赞美,当有人点击扩展并将其打印出来时,我随机抽取一个。我也使用 chromes tts(文本到语音)来大声说赞美。那很简单,但是....
我希望它在没有人点击 chrome 扩展程序的情况下随机给你一个赞美。这可能吗?我做了一些研究,但没有成功。这是我的代码:
popup.js:
$(document).ready(function() {
var compliments = ['You are awesome.', 'Looking good.'];
var randomCompliment = Math.floor(Math.random()*compliments.length);
$('#compliment').append('<li>' + compliments[randomCompliment] + '</li>');
chrome.tts.speak(compliments[randomCompliment])
});
清单.json:
{
"manifest_version": 2,
"name": "Complimentor",
"description": "This extension gives you a compliment.",
"version": "1.0",
"browser_action": {
"default_icon": {
"19": "icon_19.png",
"38": "icon_38.png"
},
"default_title": "Complimentor",
"default_popup": "popup.html"
},
"permissions": ["tts"]
}
popup.html:
<!doctype html>
<html>
<head>
<script src="jquery.js"></script>
<script src="popup.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="popup">
<div id="highlight"></div>
<ul id="compliment"></ul>
</div>
</body>
</html>
样式.css:
body {
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0)), color-stop(100%,rgba(0,0,0,0.1))); /* Chrome,Safari4+ */
}
.popup {
width: 300px;
font: 14px helvetica-neue, helvetica, sans-serif;
color: #666;
position: relative;
text-align: center;
}
#hightlight {
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(255,255,255,.3)), color-stop(100%, rgba(255,255,255,0))); /* Chrome,Safari4+ */
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
.popup ul {
list-style: none;
margin: 0;
padding: 0;
}
.popup li {
padding: 3px 0;
}