从这段代码的JavaScript不像预期的那样工作,当我把它变成一个Chrome扩展
The JavaScript from this code is not working as expected when I made it into a Chrome Extension
这是代码:
HTML:<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<title>New Tab</title>
</head>
<body id="body" onload="showDate()">
<div id="timeDiv"> </div>
<div id="secondsDiv"> </div>
<div id="quoteDiv" onload="randomQuote()"> </div>
<a href="intro.html">
<div class="element"></div>
<p class="logo">a</p>
</a>
<p class="credits">Made by Jaiveer Chadda </p>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
Javascript: /////////////////////////////////////////////// Q U O T E ///////////////////////////////////////////////
window.onload = function() {
var randNumForQuote = Math.floor((Math.random() * 11));
if (randNumForQuote == 0) {
document.getElementById("quoteDiv").innerHTML = '<span>“ </span>Life is like riding a bicycle.<br>To keep your balance, you must keep moving.<span>”</span><br> - Albert Einstein';
} else if (randNumForQuote == 1) {
document.getElementById("quoteDiv").innerHTML = '<span>“ </span>You only live once, but if you do it right, once is enough.<span>”</span><br> - Mae West';
} else if (randNumForQuote == 2) {
document.getElementById("quoteDiv").innerHTML = '<span>“ </span>Everyone is a genius, but if you judge a fish by it''s<br> ability to climb a tree, it will live it''s whole life<br> believing that it is stupid.<span>”</span><br> - Albert Einstein';
} else if (randNumForQuote == 3) {
document.getElementById("quoteDiv").innerHTML = '<span>“ </span>Be the change you want to see in the world.<span>”</span><br> - Mahatma Gandhi';
} else if (randNumForQuote == 4) {
document.getElementById("quoteDiv").innerHTML = '<span>“ </span>To live is the rarest thing in the world.<br> Most people exist, that is all.<span>”</span><br> - Oscar Wilde';
} else if (randNumForQuote == 5) {
document.getElementById("quoteDiv").innerHTML = '<span>“ </span>No one ever drowned in sweat<span>”</span><br> - British Naval Saying';
} else if (randNumForQuote == 6) {
document.getElementById("quoteDiv").innerHTML = '<span>“ </span>Simplicity is the ultimate sophistication<span>”</span><br> - Leonardo Da Vinci';
} else if (randNumForQuote == 7) {
document.getElementById("quoteDiv").innerHTML = '<span>“ </span>Fall seven times, stand up eight.<span>”</span><br> - Japanese proverb';
} else if (randNumForQuote == 8) {
document.getElementById("quoteDiv").innerHTML = '<span>“ </span>He who learns but does not think is lost! <br>He who thinks but does not learn is in great danger.<span>”</span><br> - Confucius';
} else if (randNumForQuote == 9) {
document.getElementById("quoteDiv").innerHTML = '<span>“ </span>I cried because I had no shoes, Till I saw a man <br>with no feet. Life is full of blessings,<br>Sometimes we are just too blind to see them...<span>”</span><br> - Unknown';
} else if (randNumForQuote == 10) {
document.getElementById("quoteDiv").innerHTML = '<span>“ </span>Live as if you were to die tomorrow.<br>Learn as if you were to live forever<span>”</span><br> - Mahatma Gandhi';
}
}
//////////////////////////////////////////////// T I M E ////////////////////////////////////////////////
function showDate() {
var now = new Date();
var date = ((now.getDate() < 10) ? "0" : "") + now.getDate();
function fourdigits(number) {
return (number < 1000) ? number + 1900 : number;
}
tnow = new Date();
thour = now.getHours();
tmin = now.getMinutes();
tsec = now.getSeconds();
if (tmin <= 9) { tmin = "0" + tmin; }
if (thour < 10) { thour = "0" + thour; }
if (thour > 12) {
thour = thour - 12;
}
if (thour < 10){
thour = "0" + thour ;
} else {
thour = "" + thour ;
}
if (tsec % 2 == 1){
today = thour + "   " + tmin;
} else {
today = thour + " : " + tmin;
}
if (tsec < 10){
now = "0" + tsec ;
} else {
now = "" + tsec ;
}
document.getElementById("timeDiv").innerHTML = today;
document.getElementById("secondsDiv").innerHTML = now;
}
setInterval("showDate()", 1000);
当我将这段代码作为本地文件运行时,它的所有工作都像它应该的那样。时间和报价都在工作,时间每秒钟都在刷新。
但是当我使它成为一个本地chrome扩展,只有报价是工作的。
请帮助。
您违反了对扩展强制执行的几项内容安全策略。
通读文档。
-
不能使用内联代码:禁止在HTML中设置
onload="..."
。如果您确实需要绑定到load
事件(在这种情况下这是非常值得怀疑的),请使用addEventListener
。考虑切换到DOMContentLoaded
事件。 -
你的
setInterval("showDate()", 1000);
调用是一个隐式的eval
:你强迫JavaScript引擎解析字符串来执行,而不是使用一个函数的引用,完全没有必要。这个很容易修复:setInterval(showDate, 1000);
相关文章:
- 从Chrome扩展名中的popup.html文件在background.js文件中运行一个函数
- 在这个使用hasOwnProperty的对象扩展程序中有一个错误,I'我不确定那个bug是什么,也不确定这个扩展
- 打开一个选项卡,并在firefox网络扩展中向其发出POST请求
- 制作一个chrome扩展,替换css和js元素
- Chrome扩展和Angular:我可以把Chrome.runtime.sendMessage变成一个同步函数吗
- Firefox扩展开发:如何在内容脚本中创建一个全局函数,以便其他加载的脚本文件可以访问它
- 检测是否从另一个扩展安装了一个扩展
- 在一个扩展了Sonata管理twitch/Symfony 2的twitch中调用javascript(ExtJS)的问题
- 如何将消息从 chrome 扩展程序发送到另一个扩展程序
- 如果 Chrome 扩展程序崩溃,如何让另一个扩展程序重新加载崩溃的扩展程序
- 使用交叉骑手时,使用多个文件而不是一个扩展名.js文件
- 定义一个扩展Function的类
- 如何用另一个扩展现有的js数组
- 创建一个扩展另一个对象的对象
- 如何构建一个扩展,以保存HTML表单的值到电子表格使用应用程序脚本
- 用Javascript编写一个扩展方法
- 使用webRequest来取消由另一个扩展发出的请求
- 我可以写一个“扩展”吗?Javascript中的继承函数
- Chrome扩展:让一个扩展监听页面上的事件
- 解析平台-调用JavaScript函数定义在一个扩展字符串对象得到错误对象没有方法