Skip to content

Instantly share code, notes, and snippets.

@jonathantneal
Created July 6, 2014 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathantneal/a82600f12804589c0f0b to your computer and use it in GitHub Desktop.
Save jonathantneal/a82600f12804589c0f0b to your computer and use it in GitHub Desktop.
IndieAuth.js
(function (NAME, SEPARATOR) {
var
ATTR = 'data-' + NAME,
SELECTOR = '[' + ATTR + ']';
function onupdate() {
// for all matching elements
Array.prototype.forEach.call(document.querySelectorAll(SELECTOR), function (element) {
var
// get data from attribute
data = element.getAttribute(ATTR),
// detect separator
separatorIndex = data.indexOf(SEPARATOR) !== -1 ? data.indexOf(SEPARATOR) : data.length,
// separate feature from options
feature = data.slice(0, separatorIndex),
options = data.slice(separatorIndex + 1);
// remove attribute
element.removeAttribute(ATTR);
// do something with the feature and options
console.log(feature, options);
});
}
// update now or when DOM is ready
if (/e/.test(document.readyState)) onupdate();
else document.addEventListener('DOMContentLoaded', onupdate);
// update when DOM changes
document.addEventListener('DOMSubtreeModified', onupdate);
})('indieauth', ':');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment