0

How can I create and edit a css file in browser Dom by javascript and then download it?

I have created a button creator, and there are tons of css rules, and the css file is too long. So, I want to give the user a choice, and when the user makes a button I want to give a css file only with that css rules that is used for creating a button. The css file will be created in browser dom by javascript.

For example, let's say I have created a button with

{
    font-size: 20px;
    padding:10px 5px;
    background-color: #333;
    color: #FFF;
}

etc.

Now, I want a css file with only this style.
I don't want to give the option for copy the styles in clipboard.
The user will get a css file to download.

I don't know any back end language. I am in learning process of front end.

Jota
  • 17,281
  • 7
  • 63
  • 93
Shohidul Alam
  • 653
  • 1
  • 6
  • 19
  • This [Stack Overflow question](http://stackoverflow.com/questions/3665115/create-a-file-in-memory-for-user-to-download-not-through-server) should get you started – Duncan Tidd Feb 03 '16 at 13:45
  • Thanks http://stackoverflow.com/users/1980330/duncan-tidd. But I dont know any back end language – Shohidul Alam Feb 03 '16 at 13:53
  • you might be interested in something like this, however this is done with coffeescript http://codepen.io/jakealbaugh/pen/PwLXXP – bulanmaster Feb 03 '16 at 15:53
  • and also this (the export part) for giving the user a file to save http://codepen.io/bulanmaster/pen/jbQMQa (as far as i remember you can send file to be saved in any extension) – bulanmaster Feb 03 '16 at 15:56

1 Answers1

0

Here you go getting the computed style for a given element and giving the user the option to save this in a css file. There can be made tweaks to it of course, but basically this is it:

HTML:

<button id="b" href="#">export to CSV</button>

CSS:

#b {
    background: linear-gradient(to bottom right, #fff, #0af);
    border: solid 1px #0af;
    border-radius: 10px;
    color: #333;
    cursor: pointer;
    margin: 10px;
    outline: none;
}
#b:hover {
    background: linear-gradient(to bottom right, #fff, #0af, #07c);
}

and Javascript (JQuery):

b = $('#b');
function css(a) {
    var sheets = document.styleSheets,
        o = {};
    for (var i in sheets) {
        var rules = sheets[i].rules || sheets[i].cssRules;
        for (var r in rules) {
            if (a.is(rules[r].selectorText)) {
                o = $.extend(o, css2json(rules[r].style), css2json(a.attr('style')));
            }
        }
    }
    return o;
}
function css2json(css) {
    var s = {};
    if (!css) return s;
    if (css instanceof CSSStyleDeclaration) {
        for (var i in css) {
            if ((css[i]).toLowerCase) {
                s[(css[i]).toLowerCase()] = (css[css[i]]);
            }
        }
    } else if (typeof css == "string") {
        css = css.split("; ");
        for (var i in css) {
            var l = css[i].split(": ");
            s[l[0].toLowerCase()] = (l[1]);
        }
    }
    return s;
}
myCSS = objToString(css(b));
function objToString(obj) {
    var str = '{\n';
    for (var p in obj) {
        if (obj.hasOwnProperty(p)) {
            str += '    ' + p + ': ' + obj[p] + ';\n';
        }
    }
    str += '}';
    return str;
}
function download(text, name, type) {
    var a = document.createElement("a");
    var file = new Blob([text], {
        type: type
    });
    a.href = URL.createObjectURL(file);
    a.download = name;
    a.click();
}
b.on('click', function() {
    download(myCSS, 'test.css', 'text')
});

oh and of course the jsFiddle to test it...

Credits going for Awesomeness01, Brett Zamir and marknadal

Community
  • 1
  • 1
bulanmaster
  • 401
  • 3
  • 15