【问题标题】:JS doesnt work in Chrome ExtensionJS 在 Chrome 扩展程序中不起作用
【发布时间】:2015-01-16 19:17:13
【问题描述】:

我的 Javascript 在我的 Google Chrome 扩展程序中不起作用。 我以前从未使用过 Google Chrome 扩展程序,也不知道如何处理这个问题。我想在我的 HTML 中嵌入 JS,但它不起作用。我们看到计算器,但按钮可以做任何事情。即使我们输入一些数字并按回车,这些数字也会被删除,什么也不会发生。

这里是我的 HTML 和 JS:

主页 HTML

<html>
    <head> 
        <title>AFMO</title>
        <style>
            body {
                min-width:200;
                min-height:400;
                overflow-x: hidden;
            }
        </style>        
    </head>
    <body>
    <a href="calculator.html">Calculator</a>



    <p>
        <id="Links">
        <a href="calculator.html" target="_Calculator2"><img src="icon/twitter.png" width="64" height="64"/></a>
        <a href="https://twitter.com/" target="_Twitter"><img src="icon/twitter.png" width="64" height="64"/></a>
        <a href="https://www.facebook.com/" target="_Facebook"><img src="icon/facebook.png" width="64" height="64"/></a>
        <a href="http://instagram.com/" target="_Instagram"><img src="icon/instagram.png" width="64" height="64"/></a>
        <a href="http://www.youtube.com/" target="_Youtube"><img src="icon/youtube.png" width="64" height="64"/></a>
        <a href="https://www.blogger.com/" target="_Blogger"><img src="icon/blogger.png" width="64" height="64"/></a>
        <a href="https://vimeo.com/" target="_Vimeo"><img src="icon/vimeo.png" width="64" height="64"/></a>
        </id>
    </p>
    </body>
</html>

链接的 HTML

    <html>
<head>
<title>Taschenrechner</title>
<style type="text/css">
.button {  width:60px; text-align:center;
           font-family:System,sans-serif;
           font-size:100%; }
.display { width:100%; text-align:right;
           font-family:System,sans-serif;
           font-size:100%; }
</style>
</head>
<body bgcolor="#FFFFE0" onload="calculator.js">

<form name="Rechner" action="" onsubmit="Ergebnis();return false;">
<table border="5" cellpadding="10" cellspacing="0">
<tr>
<td bgcolor="#C0C0C0">
<input type="text" name="Display" align="right" class="display"></td>
</tr><tr>
<td  bgcolor="#E0E0E0">
<table border="0" cellpadding="0" cellspacing="2">
<tr>
<td><input type="button" width="60" class="button" value="  7   " onclick="Hinzufuegen('7')"></td>
<td><input type="button" width="60" class="button" value="  8   " onclick="Hinzufuegen('8')"></td>
<td><input type="button" width="60" class="button" value="  9   " onclick="Hinzufuegen('9')"></td>
<td><input type="button" width="60" class="button" value="  +   " onclick="Hinzufuegen('+')"></td>
</tr>
<tr>
<td><input type="button" width="60" class="button" value="  4   " onclick="Hinzufuegen('4')"></td>
<td><input type="button" width="60" class="button" value="  5   " onclick="Hinzufuegen('5')"></td>
<td><input type="button" width="60" class="button" value="  6   " onclick="Hinzufuegen('6')"></td>
<td><input type="button" width="60" class="button" value="  -   " onclick="Hinzufuegen('-')"></td>
</tr>
<tr>
<td><input type="button" width="60" class="button" value="  1   " onclick="Hinzufuegen('1')"></td>
<td><input type="button" width="60" class="button" value="  2   " onclick="Hinzufuegen('2')"></td>
<td><input type="button" width="60" class="button" value="  3   " onclick="Hinzufuegen('3')"></td>
<td><input type="button" width="60" class="button" value="  *   " onclick="Hinzufuegen('*')"></td>
</tr>
<tr>
<td><input type="button" width="60" class="button" value="  0   " onclick="Hinzufuegen('0')"></td>
<td><input type="button" width="60" class="button" value="  .   " onclick="Hinzufuegen('.')"></td>
<td><input type="button" width="60" class="button" value="  =   " onclick="Ergebnis()"></td>
<td><input type="button" width="60" class="button" value="  /   " onclick="Hinzufuegen('/')"></td>
</tr>
<tr>
<td><input type="button" width="60" class="button" value="sqrt " onclick="Sonderfunktion('sqrt')"></td>
<td><input type="button" width="60" class="button" value=" pow " onclick="Sonderfunktion('pow')"></td>
<td><input type="button" width="60" class="button" value=" ln " onclick="Sonderfunktion('ln')"></td>
<td><input type="reset"  width="60" class="button" value="  C  "></td>
</tr>
</table>
</td></tr></table>
</form>

</body>
</html>

JS

    function Check (Eingabe) {
  var nur_das = "0123456789[]()-+*%/.";
  for (var i = 0; i < Eingabe.length; i++)
    if (nur_das.indexOf(Eingabe.charAt(i)) < 0)
      return false;
  return true;
}

function Ergebnis () {
  var x = 0;
  if (Check(window.document.Rechner.Display.value))
    x = eval(window.document.Rechner.Display.value);
  window.document.Rechner.Display.value = x;
}

function Hinzufuegen (Zeichen) {
  window.document.Rechner.Display.value = window.document.Rechner.Display.value + Zeichen;
}

function Sonderfunktion (Funktion) {
  if (Check(window.document.Rechner.Display.value)) {
    if (Funktion == "sqrt") {
      var x = 0;
      x = eval(window.document.Rechner.Display.value);
      window.document.Rechner.Display.value = Math.sqrt(x);
    }
    if (Funktion == "pow") {
      var x = 0;
      x = eval(window.document.Rechner.Display.value);
      window.document.Rechner.Display.value = x * x;
    }
    if (Funktion == "ln") {
      var x = 0;
      x = eval(window.document.Rechner.Display.value);
      window.document.Rechner.Display.value = Math.log(x);
    }
  } else
    window.document.Rechner.Display.value = 0}

更多信息: 这是我们自己的 Chrome 应用程序。我们只是复制了计算器,但我们不会发布这个应用程序,所以我想这无关紧要。

编辑 当我按计算器上的任何数字时,它会出现在控制台中。 拒绝执行内联事件处理程序,因为它违反了以下“见下文”内容安全策略指令:“script-src 'self' chrome-extension-resource:”。启用内联执行需要“unsafe-inline”关键字、哈希(“sha256-...”)或随机数(“nonce-...”)。 我已经尝试过解决这个问题,但我什至找不到任何内联脚本。

""见下文""
这里是按钮的线。如果我按 7,第 25 行会出现错误,如果我在 39 中按 3,则会出现错误。我想我必须把所有的

<td><input type="button" width="60" class="button" value="  4   " onclick="Hinzufuegen('4')"></td>

在一个额外的js中,但我真的不知道该怎么做

【问题讨论】:

  • 你能把你的问题具体说明一下吗? “我的 javascript 不起作用”是什么意思?算不算差?活动不开始?它会引发某种错误吗?我们不是巫师:)
  • 欢迎来到 Stack Overflow!这是一个专业网站,因此我删除了您问题中的一些垃圾内容 - 请接受编辑。还请说明这是否是您自己的 Chrome 扩展程序,因为这不清楚。 Chrome 开发者控制台中是否有任何内容?

标签: javascript html google-chrome


【解决方案1】:

您需要在 head 部分中添加一个脚本标签

<head>
 <script type="text/javascript" language="javascript" src="jsfilehere.js"></script>
</head>

【讨论】:

  • 这是现在 Calculator.html 的头文件,它在我们的 Chrome 扩展程序中仍然不起作用。 Taschenrechner
【解决方案2】:

您的&lt;script&gt; 标签在哪里?
它应该是这样的:

<script src="yourJSFile.js"></script>

【讨论】:

  • 它在链接的 HTML 的正文中 我们有一个 HTML,即“主页 HTML”,它是主页。有一个按钮应该加载一个新页面,链接的 HTML,然后它应该显示一个计算器。但是计算器的按钮不起作用,无论我们做什么,它都不起作用。如果有人需要,我可以上传完整的应用程序文件夹
  • 你的意思是body.onload?除非你有我提到的 ,否则它不会工作
  • 好吧,我们的 Calculator.html 中有一个 ,即“链接的 HTML”。但是还是不行
猜你喜欢
  • 2012-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多