HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Connect MetaMask and Call Contract</title>
<script src="https://cdn.jsdelivr.net/npm/web3@1.3.6/dist/web3.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
background: linear-gradient(to right, #ff7e5f, #feb47b);
color: white;
text-align: center;
padding: 20px;
}
h1, h2 {
margin-bottom: 20px;
}
button {
background: linear-gradient(to right, #6a11cb, #2575fc);
border: none;
color: white;
padding: 15px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 10px 2px;
cursor: pointer;
border-radius: 25px;
transition: background 0.3s ease;
}
button:hover {
background: linear-gradient(to right, #2575fc, #6a11cb);
}
form {
background: rgba(255, 255, 255, 0.2);
padding: 20px;
border-radius: 15px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
display: inline-block;
text-align: left;
}
label {
display: block;
margin: 10px 0 5px;
}
input[type="text"], input[type="checkbox"] {
width: calc(100% - 20px);
padding: 10px;
border: none;
border-radius: 5px;
margin-bottom: 15px;
display: block;
}
input[type="checkbox"] {
width: auto;
}
#result {
margin-top: 20px;
}
</style>
</head>
<body>
<h1>Connect MetaMask and Call Contract</h1>
<button id="connectButton">Connect MetaMask</button>
<div id="account"></div>
<h2>Call createNFAContract</h2>
<form id="contractForm">
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<label for="symbol">Symbol:</label>
<input type="text" id="symbol" name="symbol">
<h3>AppInfo</h3>
<label for="routerRequired">Router Required:</label>
<input type="checkbox" id="routerRequired" name="routerRequired">
<label for="paymentModel">Payment Model:</label>
<input type="text" id="paymentModel" name="paymentModel">
<label for="downloadURIs">Download URIs (comma separated):</label>
<input type="text" id="downloadURIs" name="downloadURIs">
<label for="codeHash">Code Hash:</label>
<input type="text" id="codeHash" name="codeHash">
<label for="abiURIs">ABI URIs (comma separated):</label>
<input type="text" id="abiURIs" name="abiURIs">
<label for="abiHash">ABI Hash:</label>
<input type="text" id="abiHash" name="abiHash">
<label for="appVersionId">Version ID:</label>
<input type="text" id="appVersionId" name="appVersionId">
<h3>VersionInfo</h3>
<label for="versionId">Version ID:</label>
<input type="text" id="versionId" name="versionId">
<label for="versionDownloadURIs">Download URIs (comma separated):</label>
<input type="text" id="versionDownloadURIs" name="versionDownloadURIs">
<label for="versionCodeHash">Code Hash:</label>
<input type="text" id="versionCodeHash" name="versionCodeHash">
<label for="versionAbiURIs">ABI URIs (comma separated):</label>
<input type="text" id="versionAbiURIs" name="versionAbiURIs">
<label for="versionAbiHash">ABI Hash:</label>
<input type="text" id="versionAbiHash" name="versionAbiHash">
<label for="initialOwner">Initial Owner Address:</label>
<input type="text" id="initialOwner" name="initialOwner">
<button type="button" id="callContractButton">Call Contract</button>
</form>
<div id="result"></div>
<script>
let web3;
let account;
const contractAddress = "0xe2e43ef883ff83307627c92153c9c356821aff9b";
const contractABI = [
{
"inputs": [
{"internalType": "string","name": "name","type": "string"},
{"internalType": "string","name": "symbol","type": "string"},
{"components": [
{"internalType": "bool","name": "routerRequired","type": "bool"},
{"internalType": "string","name": "paymentModel","type": "string"},
{"internalType": "string[]","name": "downloadURIs","type": "string[]"},
{"internalType": "bytes32","name": "codeHash","type": "bytes32"},
{"internalType": "string[]","name": "abiURIs","type": "string[]"},
{"internalType": "bytes32","name": "abiHash","type": "bytes32"},
{"internalType": "string","name": "versionId","type": "string"}
], "internalType": "struct AppInfo", "name": "appInfo", "type": "tuple"},
{"components": [
{"internalType": "string","name": "versionId","type": "string"},
{"internalType": "string[]","name": "downloadURIs","type": "string[]"},
{"internalType": "bytes32","name": "codeHash","type": "bytes32"},
{"internalType": "string[]","name": "abiURIs","type": "string[]"},
{"internalType": "bytes32","name": "abiHash","type": "bytes32"}
], "internalType": "struct VersionInfo", "name": "versionInfo", "type": "tuple"},
{"internalType": "address","name": "initialOwner","type": "address"}
],
"name": "createNFAContract",
"outputs": [{"internalType": "address","name":"","type":"address"}],
"stateMutability": "nonpayable",
"type": "function"
}
];
document.getElementById('connectButton').addEventListener('click', async () => {
if (typeof window.ethereum !== 'undefined') {
web3 = new Web3(window.ethereum);
await window.ethereum.request({ method: 'eth_requestAccounts' });
const accounts = await web3.eth.getAccounts();
account = accounts[0];
document.getElementById('account').innerText = `Connected account: ${account}`;
} else {
alert('MetaMask is not installed. Please install MetaMask and try again.');
}
});
document.getElementById('callContractButton').addEventListener('click', async () => {
if (!account) {
alert('Please connect MetaMask first.');
return;
}
const name = document.getElementById('name').value;
const symbol = document.getElementById('symbol').value;
const routerRequired = document.getElementById('routerRequired').checked;
const paymentModel = document.getElementById('paymentModel').value;
const downloadURIs = document.getElementById('downloadURIs').value.split(',');
const codeHash = document.getElementById('codeHash').value;
const abiURIs = document.getElementById('abiURIs').value.split(',');
const abiHash = document.getElementById('abiHash').value;
const appVersionId = document.getElementById('appVersionId').value;
const versionId = document.getElementById('versionId').value;
const versionDownloadURIs = document.getElementById('versionDownloadURIs').value.split(',');
const versionCodeHash = document.getElementById('versionCodeHash').value;
const versionAbiURIs = document.getElementById('versionAbiURIs').value.split(',');
const versionAbiHash = document.getElementById('versionAbiHash').value;
const initialOwner = document.getElementById('initialOwner').value;
const contract = new web3.eth.Contract(contractABI, contractAddress);
try {
const receipt = await contract.methods.createNFAContract(
name,
symbol,
{
routerRequired: routerRequired,
paymentModel: paymentModel,
downloadURIs: downloadURIs,
codeHash: codeHash,
abiURIs: abiURIs,
abiHash: abiHash,
versionId: appVersionId
},
{
versionId: versionId,
downloadURIs: versionDownloadURIs,
codeHash: versionCodeHash,
abiURIs: versionAbiURIs,
abiHash: versionAbiHash
},
initialOwner
).send({ from: account });
document.getElementById('result').innerText = `Transaction successful: ${receipt.transactionHash}`;
} catch (error) {
console.error(error);
document.getElementById('result').innerText = `Error: ${error.message}`;
}
});
</script>
</body>
</html>
Also see: Tab Triggers