javascript - Save a file to filesystem with chrome packaged app - Stack Overflow

I have a variable full of css attributes that I would like to use to create a CSS file and save it to t

I have a variable full of css attributes that I would like to use to create a CSS file and save it to the apps file directory. How would I go about this in a chrome packaged app?

I have a variable full of css attributes that I would like to use to create a CSS file and save it to the apps file directory. How would I go about this in a chrome packaged app?

Share Improve this question asked Apr 18, 2013 at 7:10 darylhedleydarylhedley 2581 gold badge8 silver badges23 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You're not going to like this answer. You can't do this, and if you figured out a way to do it, it would be considered a security hole that the Chrome team would close.

Part of the design goals of packaged apps is to make the entire application statically analyzable at the time it's uploaded to the web store. This means that when a user is making the decision whether to install the app from the store, that decision is based on what the code actually does (it contains these scripts, this CSS, and these images, and it asks for these permissions) rather than on whether you trust the developer or the website. www.example. might look trustworthy today, but tomorrow it might be sold to a new owner, or promised. So the decision "do I trust example.?" is hard. Packaged apps make that decision easier; in most cases, the "thing" you're trusting is right there, right in front of you, in the CRX you just downloaded.

If you understand this design goal, then you understand why the design of packaged apps doesn't allow for self-modifying code (which is the generic term for what you want to do). If your code could generate code, then it bees significantly harder for static analysis to determine what the app could do (either intentionally or through bugs). Along the same lines, Content Security Policy is fairly strict for packaged apps and can't be set to a less restrictive policy, which is why eval() won't work in script.

Is there a reason why your JavaScript can't set your DOM elements' style properties? That ought to give you the flexibility you want without running up against the boundaries of the platform's security model.

Edit: just saw your other question, which seems very similar to this one. Are you building a CSS editor? Or are you trying to modify your own app's CSS? My answer above assumed the second case. If I've misunderstood your question, then clarify and I'll attempt to improve the answer.

While @sowbug's answer was technically correct in that you cannot modify your apps packaged content, the OP can still achieve what is essentially the same.

Save css:

var someCSSCode = 'a { color:#de0 }';
webkitRequestFileSystem(PERSISTENT, 1024*1024, function(fileSystem) {
    fileSystem.root.getFile('my.css', {create:true}, function(file) {
        file.createWriter(function(writer) {
            this.onwriteend = function() {
                this.onwriteend = null;
                this.truncate(this.position); //in case a longer file was already here
            };
            writer.write(new Blob([someCSSCode], {type:'text/plain'}));
        });
    });
});

Load css:

webkitRequestFileSystem(PERSISTENT, 0, function(fileSystem) {
    fileSystem.root.getFile('my.css', {}, function(file) {
        var elem = document.createElement('link');
        elem.rel  = 'stylesheet';
        elem.type = 'text/css';
        elem.href = file.toURL();
        document.head.appendChild(elem); //or document.body
    });
});

Note that this code doesn't include any error handling.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745238170a4618014.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信