Skip to content

Instantly share code, notes, and snippets.

@junkycoder
Last active September 27, 2017 07:53
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 junkycoder/d52c477c6157f67a1cdf2d61a57779f0 to your computer and use it in GitHub Desktop.
Save junkycoder/d52c477c6157f67a1cdf2d61a57779f0 to your computer and use it in GitHub Desktop.
Convert CSS to JS in Marketch panel
// Can be used with Custom JavaScript for Websites
// https://chrome.google.com/webstore/detail/custom-javascript-for-web/poakhlngfciodnhlhhgnaaelnpjljija
// but is necessary to allow 'local URLs' in chrome://extensions/
if (document.title.indexOf('Marketch') === 0) {
$('.artboard').on('mouseup', () => setTimeout(init, 100));
}
function init () {
const $code = $('textarea[name="code"]');
let styles = $code.text().split('\n').map(line => (
line.replace(';', '').split(':')
));
styles = styles.map(([prop, value]) => ([
snakeToCamel(prop),
valueToJsValue(value)
]));
styles = styles.reduce((text, style) => (
`${text}${style[0]}: ${style[1]},\n`
), '');
$code.text(styles);
}
function snakeToCamel (s) {
return s.replace(/(\-\w)/g, function(m){return m[1].toUpperCase();});
}
function valueToJsValue (value) {
if (isNumericValue(value)) {
return parseFloat(value);
}
return `'${value}'`;
}
function isNumericValue (value) {
return (new RegExp(/^-?[\d]+[px]?/)).test(value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment