【问题标题】:ReferenceError: openpgp is not definedReferenceError:未定义openpgp
【发布时间】:2020-12-10 16:29:22
【问题描述】:

我正在创建一个简单的 chrome 扩展程序,它可以使用 openPGP 的库加密和解密字符串。但是,我似乎无法定义 openGPG 并不断收到错误:

ReferenceError: openpgp is not defined

我已经在我的 HTML 文件中定义了 openGPG 库,我认为它可以使其在全球范围内可用,对吗?

我的 HTML & JS 代码如下

使用我的加密的 JS 文件。非常早期的版本,但只是想在定义键和添加逻辑之前运行它。

    document.getElementById("encryptTest").addEventListener('click', () => {
        console.log("Popup DOM fully loaded and parsed");
    
        async function encryptString() {
            // put keys in backtick (``) to avoid errors caused by spaces or tabs
            const publicKeyArmored = `-----BEGIN PGP PUBLIC KEY BLOCK-----
        ...
        -----END PGP PUBLIC KEY BLOCK-----`;
            const privateKeyArmored = `-----BEGIN PGP PRIVATE KEY BLOCK-----
        ...
        -----END PGP PRIVATE KEY BLOCK-----`; // encrypted private key
            const passphrase = `yourPassphrase`; // what the private key is encrypted with
        
            const { keys: [privateKey] } = await openpgp.key.readArmored(privateKeyArmored);
            await privateKey.decrypt(passphrase);
        
            const { data: encrypted } = await openpgp.encrypt({
                message: openpgp.message.fromText('Hello, World!'),                 // input as Message object
                publicKeys: (await openpgp.key.readArmored(publicKeyArmored)).keys, // for encryption
                privateKeys: [privateKey]                                           // for signing (optional)
            });
            console.log(encrypted); // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'
            const { data: decrypted } = await openpgp.decrypt({
                message: await openpgp.message.readArmored(encrypted),              // parse armored message
                publicKeys: (await openpgp.key.readArmored(publicKeyArmored)).keys, // for verification (optional)
                privateKeys: [privateKey]                                           // for decryption
            });
            console.log(decrypted); // 'Hello, World!'
        }
    
        //We have permission to access the activeTab, so we can call chrome.tabs.executeScript:
        chrome.tabs.executeScript({
            code: '(' + encryptString + ')();' //argument here is a string but function.toString() returns function's code
        });
    });

HTML 文件

<!DOCTYPE html>
<html>
  <head>
    <style>
        body {
            width: 350px;
            height: 500px;
        }
      button {
        height: 50px;
        width: 150px;
        outline: none;
      }
      input {
        width: 95%;
        padding: 5px;
        margin-right: 15px;
        margin-top: 50px;
      }
      .decrypt {
        margin-top: 10px;
        width: 150px;
        height: 35px;
      }
    </style>

  </head>
  <body>
      <h1>AIS PGP Exam Encryption</h1>
    <button id="generateKey">Generate Key</button>
    <button id="htmlGrabber">Get HTML</button>
    <input placeholder="Private Key"></input>
    <input placeholder="Message to Decrypt"></input>
    <button class="decrypt">Decrypt Message</button>
    <button id="encryptTest">Test</button>
  </body>
  <script src="../js/jquery-3.5.1.min.js"></script>
  <script src="../js/openpgp.min.js"></script>
  <script src="../js/encrypt.js"></script>
  <script src="../js/htmlGrabber.js"></script>
</html>

【问题讨论】:

    标签: javascript html openpgp


    【解决方案1】:

    我认为问题在于该函数在您的代码中是孤立的。还要确保在您的 HTML 文件中正确定义它。欢迎来到 Stack Overflow :) !

    【讨论】:

      猜你喜欢
      • 2012-08-24
      • 2015-10-04
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多