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.
<h2 id="sample_app">Add Cue Points</h2>
<fieldset class="bcls-fieldset"><legend>Account Information</legend>
<p>By default, you will get results from Brightcove sample account.</p>
<p><button class="bcls-button" id="useMyAccount">Use My Account Instead</button></p>
<div id="basicInfo" style="display:none;">
<table class="bcls-table">
<tbody class="bcls-table__body">
<tr>
<td>Video Cloud Account (Publisher ID):</td>
<td><input id="account" type="text" size="55" /></td>
</tr>
<tr>
<td class="align-top no-wrap">Client id:</td>
<td><input id="cid" type="text" size="100" value="" /> (must include permissions for <code>video-cloud/video/update</code> and <code>video-cloud/video/read</code> or <code>video-cloud/video/all</code>)</td>
</tr>
<tr>
<td class="align-top no-wrap">Client secret:</td>
<td><input id="secret" type="text" size="100" value="" /> (must include permissions for <code>video-cloud/video/update</code> and <code>video-cloud/video/read</code> or <code>video-cloud/video/all</code>)</td>
</tr>
</tbody>
</table>
<p><button id="get_videos" class="bcls-button">Refresh Video List New Account Information</button></p>
</div>
</fieldset>
<fieldset class="bcls-fieldset"><legend>Inputs</legend>
<p>Video to update: <select id="video" style="display:inline-block;width:40%;"></select></p>
<h4>Cue point information</h4>
<p>Name: <input id="name" type="text" style="display:inline-block;width:40%;" /> <span style="color: #CC0000">Required!</span></p>
<p>Type: <select id="type" style="display:inline-block;width:40%;"><option value="AD">AD</option><option value="CODE" selected="selected">CODE</option></select></p>
<p>Time: <input id="time" type="text" placeholder="2.45" style="display:inline-block;width:40%;" /> <span style="color: #CC0000">Required!</span></p>
<p>Metadata: <input id="metadata" type="text" style="display:inline-block;width:40%;" /></p>
<p>Force stop: <input type="checkbox" id="force_stop" value="true" /></p>
<p><button id="addCue" class="bcls-button">Add Cue Point</button> <button id="setRequest" class="bcls-button">Submit Video Update Request</button></p>
</fieldset>
<h2 id="outputLog">Results</h2>
<fieldset class="bcls-fieldset"><legend>Outputs</legend>
<p>API request:<textarea id="apiRequest" class="bcls-code"></textarea></p>
<p>API request body:<textarea id="requestData" class="bcls-code"></textarea></p>
<pre class="line-numbers">
<code class="language-json" id="results">Response will appear here...</code></pre>
</fieldset>
/* * The body style is just for the
* background color of the codepen.
* Do not include in your code.
*/
body {
background-color: #111;
color: #f5f5f5;
font-family: sans-serif;
margin: 2em;
}
/*
* Styles essential to the sample
* are below
*/
.bcls-code {
color: #111;
font-family: 'Source Code Pro',monospace;
padding: 1em;
width: 90%;
min-height: 5em;
background-color: #cfcfcf;
font-size: 1rem;
}
.bcls-button {
padding: .5em;
background-color: #333;
border: 1px yellow solid;
border-radius: .5em;
color: yellow;
cursor: pointer;
}
fieldset {
color: #64AAB2;
border: 1px #64AAB2 solid;
border-radius: .5em;
}
code {
color: #E4B842;
font-family: 'Source Code Pro',monospace;
}
var BCLS = (function(window, document) {
// account info
var useMyAccount = document.getElementById('useMyAccount'),
basicInfo = document.getElementById('basicInfo'),
account = document.getElementById('account'),
cid = document.getElementById('cid'),
secret = document.getElementById('secret'),
get_videos = document.getElementById('get_videos'),
// value below is for BrightcoveLearning
// default client id and secret should be stored in the proxy
default_account_id = '57838016001',
// cuepoint fields
name = document.getElementById('name'),
type = document.getElementById('type'),
time = document.getElementById('time'),
metadata = document.getElementById('metadata'),
force_stop = document.getElementById('force_stop'),
// update info
video = document.getElementById('video'),
addCue = document.getElementById('addCue'),
setRequest = document.getElementById('setRequest'),
// request / response display fields
apiRequest = document.getElementById('apiRequest'),
requestData = document.getElementById('requestData'),
results = document.getElementById('results'),
// data objects
updateData = {},
client_id,
client_secret,
account_id,
video_id;
// set event listeners
useMyAccount.addEventListener('click', function() {
if (basicInfo.getAttribute('style') === 'display:none;') {
basicInfo.setAttribute('style', 'display:block;');
useMyAccount.textContent = 'Use Sample Account';
} else {
basicInfo.setAttribute('style', 'display:none;');
useMyAccount.textContent = 'Use My Account Instead';
}
});
addCue.addEventListener('click', function() {
var cue = {};
cue.name = name.value;
cue.type = getSelectedValue(type).value;
cue.time = parseFloat(time.value);
cue.metadata = metadata.value;
cue.force_stop = isChecked(force_stop);
updateData.cue_points.push(cue);
name.value = '';
time.value = '';
metadata.value = '';
addCue.textContent = 'Add Another Cue Point';
requestData.textContent = JSON.stringify(updateData, null, ' ');
});
setRequest.addEventListener('click', function() {
// get or set values for the request
if (account.value) {
account_id = account.value;
} else {
account_id = default_account_id;
}
if (cid.value) {
client_id = cid.value;
}
if (secret.value) {
client_secret = secret.value;
}
video_id = getSelectedValue(video).value;
createRequest('updateVideo');
});
get_videos.addEventListener('click', function() {
if (account.value && cid.value && secret.value) {
account_id = account.value;
client_id = cid.value;
client_secret = secret.value;
createRequest('getVideos');
} else {
alert('The account id, client id, and client secret are required if you wish to use your own account');
}
});
/**
* get selected value for single select element
* @param {htmlElement} e the select element
* @return {Object} object containing the `value`, text, and selected `index`
*/
function getSelectedValue(e) {
var selected = e.options[e.selectedIndex],
val = selected.value,
txt = selected.textContent,
idx = e.selectedIndex;
return {
value: val,
text: txt,
index: idx
};
}
/**
* determines if checkbox is checked
* @param {htmlElement} e the checkbox to check
* @return {Boolean} true if box is checked
*/
function isChecked(e) {
if (e.checked) {
return true;
}
return false;
}
/**
* createRequest sets up requests, send them to makeRequest(), and handles responses
* @param {string} type the request type
*/
function createRequest(type) {
var options = {},
cmsBaseURL = 'https://cms.api.brightcove.com/v1/accounts/' + account_id,
endpoint,
responseDecoded,
i,
iMax,
el,
txt;
// set credentials and proxy url
// if no client id and secret entered, let the proxy use defaults
if (cid.value.length > 0 && secret.value.length > 0) {
options.client_id = cid.value;
options.client_secret = secret.value;
}
options.proxyURL = 'https://solutions.brightcove.com/bcls/bcls-proxy/bcls-proxy-v2.php';
switch (type) {
case 'getVideos':
endpoint = '/videos?q=playable:true&limit=20';
options.url = cmsBaseURL + endpoint;
options.requestType = 'GET';
makeRequest(options, function(response) {
responseDecoded = JSON.parse(response);
// add options to the video selector
if (Array.isArray(responseDecoded)) {
// remove existing options
iMax = video.length;
for (i = 0; i < iMax; i++) {
video.remove(i);
}
// add new options
iMax = responseDecoded.length;
for (i = 0; i < iMax; i++) {
el = document.createElement('option');
el.setAttribute('value', responseDecoded[i].id);
if (i === 0) {
el.setAttribute('selected', 'selected');
}
txt = document.createTextNode(responseDecoded[i].name);
el.appendChild(txt);
video.appendChild(el);
}
}
});
break;
case 'updateVideo':
endpoint = '/videos/' + video_id;
options.url = cmsBaseURL + endpoint;
options.requestType = 'PATCH';
options.requestBody = JSON.stringify(updateData);
apiRequest.textContent = options.url;
requestData.textContent = JSON.stringify(updateData, null, ' ');
makeRequest(options, function(response) {
responseDecoded = JSON.parse(response);
results.textContent = JSON.stringify(responseDecoded, null, ' ');
});
break;
// additional cases
default:
console.log('Should not be getting to the default case - bad request type sent');
break;
}
}
/**
* send API request to the proxy
* @param {Object} options for the request
* @param {String} options.url the full API request URL
* @param {String="GET","POST","PATCH","PUT","DELETE"} requestData [options.requestType="GET"] HTTP type for the request
* @param {String} options.proxyURL proxyURL to send the request to
* @param {String} options.client_id client id for the account (default is in the proxy)
* @param {String} options.client_secret client secret for the account (default is in the proxy)
* @param {JSON} [options.requestBody] Data to be sent in the request body in the form of a JSON string
* @param {Function} [callback] callback function that will process the response
*/
function makeRequest(options, callback) {
var httpRequest = new XMLHttpRequest(),
response,
requestParams,
dataString,
proxyURL = options.proxyURL,
// response handler
getResponse = function() {
try {
if (httpRequest.readyState === 4) {
if (httpRequest.status >= 200 && httpRequest.status < 300) {
response = httpRequest.responseText;
// some API requests return '{null}' for empty responses - breaks JSON.parse
if (response === '{null}') {
response = null;
}
// return the response
callback(response);
} else {
alert('There was a problem with the request. Request returned ' + httpRequest.status);
}
}
} catch (e) {
alert('Caught Exception: ' + e);
}
};
/**
* set up request data
* the proxy used here takes the following request body:
* JSON.strinify(options)
*/
// set response handler
httpRequest.onreadystatechange = getResponse;
// open the request
httpRequest.open('POST', proxyURL);
// set headers if there is a set header line, remove it
// open and send request
httpRequest.send(JSON.stringify(options));
}
function init() {
// array for cue point data
updateData.cue_points = [];
// initially get videos from BrightcoveLearning account
account_id = default_account_id;
createRequest('getVideos');
}
init();
})(window, document);
Also see: Tab Triggers