javascript - HTML/JS 代码可以在普通浏览器 View 中运行,但无法作为 Chrome 扩展运行?

标签 javascript html google-chrome google-chrome-extension

我想做一个简单的扩展来节省简短的粘贴。我只是在将粘贴放入表单中会更改表单正下方的 div 的innerHTML;不过,这不是我遇到的问题。

我的问题是,当我尝试使用 Chrome 扩展中的代码时,它无法更改表单下 div 的innerHTML,但是当我在浏览器上简单地运行代码时,代码会按照我的意图执行正如我所期望的那样去做和工作。

是我的代码有问题吗?是否需要使用 Chrome 扩展执行任何额外操作才能使我的 JavaScript 代码正常工作?任何事情都有帮助!

谢谢

代码: HTML:

<!doctype html>
<html>
    <head>
        <title>Getting Started Extension's Popup</title>
        <style>
            body {
                min-width: 357px;
                overflow: hidden;
                height: 700px;
                background-color: #F7F8E0;
            }

        </style>

    <!--
      - JavaScript and HTML must be in separate files: see our Content Security
      - Policy documentation[1] for details and explanation.
      -
      - [1]: http://developer.chrome.com/extensions/contentSecurityPolicy.html
     -->
    <!--<script src="popup.js"></script>-->
    <script src="pastebook.js"></script>
    </head>
    <body>
        <form name="newpaste">
            New Paste: <input type="text" name="pasteform" value="Paste your new Paste">
            <button type="button" onclick="newPaste()">Save Paste</button>
        </form>

        <div id="bookofpastes"></div>
    </body>
</html>

Javascript:

function newPaste() {
    newpaste=document.newpaste.pasteform.value;
    document.getElementById("bookofpastes").innerHTML=newpaste;
}

如果有帮助的话,我的manifest.json文件:

{
    "manifest_version": 2,

    "name": "pastebook",
    "description": "Keep a list of all your pastes here",
    "version": "0.1",

    "browser_action": {
        "default_icon": "book2.png",
        "default_popup": "pastebook.html"
    }
}    

最佳答案

显然你不能在 list 版本 2 中使用内联 javascript 代码:

https://groups.google.com/a/chromium.org/forum/#!topic/chromium-apps/BEAGuCsX4pU

关于javascript - HTML/JS 代码可以在普通浏览器 View 中运行,但无法作为 Chrome 扩展运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23462756/

相关文章:

html - C/HTML - 从 HTML 表单打印用户名和密码

javascript - 使用 javascript 覆盖 chrome 默认快捷方式

selenium - 实现jenkins docker image + chrome + ruby​​未知错误时出现问题:Chrome无法启动:崩溃(Selenium::WebDriver::Error::UnknownError)

javascript - 为javascript点击事件传入参数

javascript - jwt_authentication 请求 ajax - symfony

php - 我如何在 php 脚本中制作这样的示例 json?

javascript - 发送空白电子邮件的 PHP 脚本无法获取发布数据

javascript - 文本框值未传递给 api 调用

css - 子菜单在 Chrome 中敏感,但在 Firefox 中很好

javascript - 单击图像时如何运行javascript函数?