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.
<section class="bcls-section">
<h2 id="Set_default_profile_app">Add Ingest Profile to Multiple Accounts</h2>
<p>You can use the form below to select a custom ingest profile from one account and add it to one or more other accounts. There is also an option to make this profile the default for each of the other accounts.</p>
<fieldset><legend>Input</legend>
<p>If you do not enter account information, Brightcove sample accounts will be used.</p>
<p><strong style="color:#E4B842">Source</strong> Account id: <input id="account_id_input" type="text" placeholder="1752604059001" /><br>(This is the account that has to profile you want to add to other accounts)</p>
<p><strong style="color:#E4B842">Target</strong> account ids (comma-delimited list): <input id="account_ids_input" type="text" placeholder="1752604059001,921483702001,57838016001" size="60" /></p>
<p>Client id: <input id="client_id_input" type="text" size="60" placeholder="c5d0a622-5479-46d8-8d8a-5f034b943fab" /></p>
<p>Client secret: <input id="client_secret_input" type="text" size="60" placeholder="w7NQYu0vUloM4GYYy2SXAxrvyFpt8fwI35qAFZcS13-VIgs0itwKNsAwHOS80sOWKJ1BUwHIvSFG" /></p>
<p><button class="bcls-button" id="getProfiles">Get Profiles</button></p>
<p>Profile to set as default: <select name="profileSelect" id="profileSelect" style="width:200px;" class="disabled">
<option value="null">Select the Profile to Add to Other Accounts</option>
</select></p>
<p><input type="checkbox" id="setIngestionDefault" /> <label for="setIngestionDefault">Set the Selected Profile as Default for ingestion for these Accounts</label></p>
<p><input type="checkbox" id="setClippingDefault" /> <label for="setClippingDefault">Set the Selected Profile as Default for clipping for these Accounts</label></p>
<p><button class="bcls-button disabled" id="addProfile">Add the Selected Profile to the Target Accounts</button></p>
</fieldset>
<fieldset><legend>Output</legend>
<p id="logger" style="color:rgb(237, 104, 38)">Waiting for input...</p>
<h4>Current API request</h4>
<pre>
<code id="apiRequest">API request will appear here...</code></pre>
<h4>Request type</h4>
<pre>
<code id="apiRequestType">API request type will appear here...</code></pre>
<h4>Response data</h4>
<pre>
<code id="apiResponse" style="width:95%;height:30em;">Response will appear here...</code></pre>
</fieldset>
</section>
/* * 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;
}
.disabled {
opacity: .4;
cursor: not-allowed;
}
var BCLS = (function(window, document) {
var // account stuff
account_id,
account_ids,
bcToken,
client_id,
client_secret,
selectedProfile,
// api stuff
proxyURL = 'https://solutions.brightcove.com/bcls/bcls-proxy/bcls-proxy-v2.php',
ipURL = 'https://ingestion.api.brightcove.com/v1/accounts/',
ipAccountSuffix = '/configuration',
ipProfileSuffix = '/profiles',
totalCalls = 0,
callNumber = 0,
accountsArray = [],
defaultAccounts = ['1485884786001', '1937897674001'],
defaultAccount = '1752604059001',
profilesArray = [],
newProfileId,
// element references
account_id_input = document.getElementById('account_id_input'),
account_ids_input = document.getElementById('account_ids_input'),
client_id_input = document.getElementById('client_id_input'),
client_secret_input = document.getElementById('client_secret_input'),
getProfiles = document.getElementById('getProfiles'),
profileSelect = document.getElementById('profileSelect'),
addProfile = document.getElementById('addProfile'),
setIngestionDefault = document.getElementById('setIngestionDefault'),
setClippingDefault = document.getElementById('setClippingDefault'),
logger = document.getElementById('logger'),
apiRequest = document.getElementById('apiRequest'),
apiResponse = document.getElementById('apiResponse'),
apiRequestType = document.getElementById('apiRequestType'),
// for filtering ingest profiles list
live_profiles = ['Live - Standard', 'Live - HD', 'Live - Premium HD'];
/**
* remove spaces from a string
* @param {String} str string to process
*/
function removeSpaces(str) {
str = str.replace(/\s/g, '');
return str;
}
/**
* determines whether an object has a certain property
* @param {object} obj the object
* @param {string} prop the property name
* @returns {boolean}
*/
function hasProperty(obj, prop) {
if (prop in obj) {
return true;
}
return false;
}
/**
* determines whether specified item is in an array
*
* @param {array} array to check
* @param {string} item to check for
* @return {boolean} true if item is in the array, else false
*/
function arrayContains(arr, item) {
var i,
iMax = arr.length;
for (i = 0; i < iMax; i++) {
if (arr[i] === item) {
return true;
}
}
return false;
}
/*
* tests to see if a string is json
* @param {String} str string to test
* @return {Boolean}
*/
function isJson(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
/**
* tests for all the ways a variable might be undefined or not have a value
* @param {String|Number} x the variable to test
* @return {Boolean} true if variable is defined and has a value
*/
function isDefined(x) {
if (x === "" || x === null || x === undefined) {
return false;
}
return true;
}
/**
* get selected value for single select element
* @param {htmlElement} e the select element
*/
function getSelectedValue(e) {
return e.options[e.selectedIndex].value;
}
/**
* 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;
}
/**
* get a copy of (rather than reference to) an object
* @param {object} obj - the object you want a copy
* @return {object} the copy
*/
function copyObj(obj) {
return JSON.parse(JSON.stringify(obj));
}
/**
* find index of an object in array of objects
* based on some property value
*
* @param {array} targetArray array to search
* @param {string} objProperty object property to search
* @param {string} value of the property to search for
* @return {integer} index of first instance if found, otherwise returns -1
*/
function findObjectInArray(targetArray, objProperty, value) {
var i, totalItems = targetArray.length,
objFound = false;
for (i = 0; i < totalItems; i++) {
if (targetArray[i][objProperty] === value) {
objFound = true;
return i;
}
}
if (objFound === false) {
return -1;
}
}
/**
* disables an element so user can't click on it
* @param {htmlElement} el the element
*/
function disableElement(el) {
el.setAttribute('disabled', 'disabled');
el.classList.add('disabled');
}
/**
* re-enables an element to make it clickable
* @param {htmlElement} el the element
*/
function enableElement(el) {
el.removeAttribute('disabled');
el.classList.remove('disabled');
}
/**
* populate a selector element from an array of objects
* @param {htmlElement} selector reference to the selector element
* @param {array} dataArray an array of objects
* @param {string} valueField name of the object field to use for option values
* @param {string} textField name of the object field to be use for the option text (can be the same as the valueField)
*/
function populateSelector(selector, dataArray, valueField, textField) {
var i,
iMax,
option,
item,
frag = document.createDocumentFragment();
iMax = dataArray.length;
for (i = 0; i < iMax; i++) {
item = dataArray[i];
option = document.createElement('option');
option.setAttribute('value', item[valueField]);
option.textContent = item[textField];
frag.appendChild(option);
}
selector.appendChild(frag);
return;
}
/**
* remove or add obsolete, standard and live profiles from the profiles list
*/
function filterProfiles() {
// below are the obsolete profiles - you just have to know their names
var deprecated_profiles = ['balanced-nextgen-player', 'Express Standard', 'mp4-only', 'balanced-high-definition', 'low-bandwidth-devices', 'balanced-standard-definition', 'single-rendition', 'Live - Standard', 'high-bandwidth-devices', 'Live - Premium HD', 'Live - HD', 'videocloud-default-trial', 'screencast'],
live_profiles = ['Live - Standard', 'Live - HD', 'Live - Premium HD'],
i = profilesArray.length;
while (i > 0) {
i--;
if (arrayContains(deprecated_profiles, profilesArray[i].name)) {
profilesArray.splice(i, 1);
} else if (profilesArray[i].brightcove_standard === true) {
profilesArray.splice(i, 1);
} else if (arrayContains(live_profiles, profilesArray[i].name)) {
profilesArray.splice(i, 1);
}
}
return;
}
/**
* sets up the data for the API request
* @param {String} id the id of the button that was clicked
*/
function setOptions(id, type) {
var i,
iMax,
endPoint,
options = {},
configurationId;
logger.textContent = 'Getting profiles for source account'
options.proxyURL = proxyURL;
if (isDefined(client_id) && isDefined(client_secret)) {
options.client_id = client_id;
options.client_secret = client_secret;
}
switch (id) {
case 'getProfiles':
var profile;
endPoint = account_id;
options.url = ipURL + endPoint + ipProfileSuffix;
options.requestType = type;
apiRequest.textContent = options.url;
apiRequestType.textContent = options.type;
makeRequest(options, function(response) {
if (isJson(response)) {
profilesArray = JSON.parse(response);
// filter out non-custom profiles
filterProfiles();
// check for display_name and if none, use name
// amd remove fields that can't be used when adding the profile to another account
iMax = profilesArray.length;
for (i = 0; i < iMax; i++) {
profile = profilesArray[i];
if (!hasProperty(profile, 'display_name')) {
profile.display_name = profile.name;
}
delete profile.date_created;
delete profile.date_last_modified;
delete profile.version;
}
// populate the profile selector
populateSelector(profileSelect, profilesArray, 'id', 'display_name');
}
apiResponse.textContent = JSON.stringify(profilesArray, null, 2);
enableElement(profileSelect);
});
break;
case 'addProfile':
var requestBody = copyObj(selectedProfile),
responseObj;
logger.textContent = 'Adding profile to target account ' + accountsArray[callNumber];
delete requestBody.id;
requestBody.account_id = accountsArray[callNumber];
endPoint = accountsArray[callNumber];
options.url = ipURL + endPoint + ipProfileSuffix;
options.requestType = type;
options.requestBody = JSON.stringify(requestBody);
apiRequest.textContent = options.url;
apiRequestType.textContent = options.type;
makeRequest(options, function(response) {
if (isJson(response)) {
responseObj = JSON.parse(response);
apiResponse.textContent = JSON.stringify(responseObj, null, 2);
newProfileId = responseObj.id;
if (isChecked(setIngestionDefault) || isChecked(setClippingDefault)) {
if (isDefined(newProfileId)) {
// if new profile id does not exist, there was an error, probably a conflict because a profile with that name already existed - so don't attempt to set default profile for this account
setOptions('getDefault', 'GET');
}
} else {
callNumber++;
if (callNumber < totalCalls) {
setOptions('addProfile', 'POST');
} else {
logger.textContent = 'All finished!'
}
}
}
});
break;
case 'getDefault':
var parsedData;
logger.textContent = 'Getting account configuration: ' + accountsArray[callNumber];
endPoint = accountsArray[callNumber];
options.url = ipURL + endPoint + ipAccountSuffix;
options.requestType = type;
apiRequest.textContent = options.url;
apiRequestType.textContent = options.type;
makeRequest(options, function(response) {
parsedData = JSON.parse(response);
apiResponse.textContent = JSON.stringify(parsedData, null, 2);
configurationId = parsedData.id;
setOptions('setDefault', 'POST')
});
break;
case 'setDefault':
var reqBody = {},
now;
logger.textContent = 'Processing account: ' + accountsArray[callNumber];
endPoint = accountsArray[callNumber];
options.url = ipURL + endPoint + ipAccountSuffix;
options.requestType = type;
if (isChecked(setIngestionDefault)) {
reqBody.default_profile_id = newProfileId;
}
if (isChecked(setClippingDefault)) {
reqBody.default_social_clipping_profile_id = newProfileId;
}
if (type === 'PUT') {
reqBody.id = configurationId;
}
reqBody.account_id = accountsArray[callNumber];
options.requestBody = JSON.stringify(reqBody);
apiRequest.textContent = options.url;
apiRequestType.textContent = options.type;
makeRequest(options, function(response) {
parsedData = JSON.parse(response);
apiResponse.textContent = JSON.stringify(parsedData, null, 2);
if (Array.isArray(parsedData)) {
// we have an error, most likely a conflict because default has already been set - try update instead
setOptions('setDefault', 'PUT');
} else {
callNumber++;
if (callNumber < totalCalls) {
setOptions('addProfile', 'POST');
} else {
logger.textContent = 'All finished!'
}
}
});
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,
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.stringify(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() {
// event handlers
getProfiles.addEventListener('click', function() {
var accountIds;
// get the inputs
client_id = client_id_input.value;
client_secret = client_secret_input.value;
newProfile = getSelectedValue(profileSelect);
// only use entered account id if client id and secret are entered also
if (isDefined(client_id) && isDefined(client_secret)) {
if (isDefined(account_id_input.value) && isDefined(account_ids_input.value)) {
account_id = account_id_input.value;
accountIds = removeSpaces(account_ids_input.value);
accountsArray = accountIds.split(',');
} else {
window.alert('To use your own account, you must specify an account id, and client id, and a client secret - since at least one of these is missing, a sample account will be used');
account_id = defaultAccount;
client_id = '';
client_secret = '';
accountsArray = defaultAccounts;
}
} else {
account_id = defaultAccount;
accountsArray = defaultAccounts;
}
totalCalls = accountsArray.length;
setOptions('getProfiles', 'GET');
});
profileSelect.addEventListener('change', function() {
var selected = getSelectedValue(profileSelect),
idx = findObjectInArray(profilesArray, 'id', selected);
selectedProfile = profilesArray[idx];
enableElement(addProfile);
});
addProfile.addEventListener('click', function() {
setOptions('addProfile', 'POST');
})
}
// kick things off
init();
})(window, document);
Also see: Tab Triggers