簡體   English   中英

當我將其插入Chrome擴展程序后,此代碼中的JavaScript無法正常工作

[英]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"> &nbsp; &nbsp; &nbsp; </div>
    <div id="secondsDiv"> &nbsp; &nbsp; </div>
    <div id="quoteDiv" onload="randomQuote()"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>
    <a href="intro.html">
      <div class="element"></div>
      <p class="logo">a</p>
    </a>
    <p class="credits">Made by Jaiveer Chadda &nbsp; &nbsp; </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>&ldquo; </span>Life is like riding a bicycle.<br>To keep your balance, you must keep moving.<span>&rdquo;</span><br> - Albert Einstein';
          } else if (randNumForQuote == 1) {
            document.getElementById("quoteDiv").innerHTML = '<span>&ldquo; </span>You only live once, but if you do it right, once is enough.<span>&rdquo;</span><br> - Mae West';
          } else if (randNumForQuote == 2) {
            document.getElementById("quoteDiv").innerHTML = '<span>&ldquo; </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>&rdquo;</span><br> - Albert Einstein';
          } else if (randNumForQuote == 3) {
            document.getElementById("quoteDiv").innerHTML = '<span>&ldquo; </span>Be the change you want to see in the world.<span>&rdquo;</span><br> - Mahatma Gandhi';
          } else if (randNumForQuote == 4) {
            document.getElementById("quoteDiv").innerHTML = '<span>&ldquo; </span>To live is the rarest thing in the world.<br> Most people exist, that is all.<span>&rdquo;</span><br> - Oscar Wilde';
          } else if (randNumForQuote == 5) {
            document.getElementById("quoteDiv").innerHTML = '<span>&ldquo; </span>No one ever drowned in sweat<span>&rdquo;</span><br> - British Naval Saying';
          } else if (randNumForQuote == 6) {
            document.getElementById("quoteDiv").innerHTML = '<span>&ldquo; </span>Simplicity is the ultimate sophistication<span>&rdquo;</span><br> - Leonardo Da Vinci';
          } else if (randNumForQuote == 7) {
            document.getElementById("quoteDiv").innerHTML = '<span>&ldquo; </span>Fall seven times, stand up eight.<span>&rdquo;</span><br> - Japanese proverb';
          } else if (randNumForQuote == 8) {
            document.getElementById("quoteDiv").innerHTML = '<span>&ldquo; </span>He who learns but does not think is lost! <br>He who thinks but does not learn is in great danger.<span>&rdquo;</span><br> - Confucius';
          } else if (randNumForQuote == 9) {
            document.getElementById("quoteDiv").innerHTML = '<span>&ldquo; </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>&rdquo;</span><br> - Unknown';
          } else if (randNumForQuote == 10) {
            document.getElementById("quoteDiv").innerHTML = '<span>&ldquo; </span>Live as if you were to die tomorrow.<br>Learn as if you were to live forever<span>&rdquo;</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 + " &nbsp "  + 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM