ReferenceError: openpgp is not defined
ReferenceError: openpgp is not defined
我正在创建一个简单的 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>
我认为问题在于该函数在您的代码中是孤立的。还要确保在 HTML 文件中正确定义它。欢迎来到 Stack Overflow :) !
我正在创建一个简单的 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>
我认为问题在于该函数在您的代码中是孤立的。还要确保在 HTML 文件中正确定义它。欢迎来到 Stack Overflow :) !