HTML/JS代码在普通浏览器视图下工作,但不能作为Chrome扩展工作

HTML/JS code works in plain browser view, but fails to work as a Chrome Extension?

本文关键字:工作 但不能 扩展 Chrome 视图 代码 JS 浏览器 HTML      更新时间:2023-09-26

我想做一个简单的扩展,可以保存短粘贴。我只是在将粘贴到表单中会改变表单下面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;
}

如果有帮助的话,我的清单。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"
    }
}    

显然你不能使用内联javascript代码与清单版本2:

https://groups.google.com/a/chromium.org/forum/!主题/chromium-apps BEAGuCsX4pU

相关文章: