我正在创建一个简单的chrome扩展,它可以使用openPGP的库对字符串进行加密和解密。然而,我似乎不能定义openGPG并不断得到错误:
ReferenceError: openpgp is not defined我已经在我的openGPG文件中定义了HTML库,我认为它可以在全球范围内使用,对吗?
我的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>发布于 2020-12-12 00:58:18
我认为问题在于你的代码中的函数是孤立的。还要确保在您的HTML文件中正确定义它。欢迎使用Stack Overflow :)!
https://stackoverflow.com/questions/65238492
复制相似问题