Windows用户名变量和JavaScript [英] Windows user name variable and javascript

查看:164
本文介绍了Windows用户名变量和JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在几个星期之前询问了这个问题之前,最近又回来解决它。我有一个隐藏的输入,如下所示:

 < input style =display:none; class =fileDialogtype =filenwworkingdir =C:\Users\nwsaveas /> 

这会在 nwworkingdir 。我想默认位置是用户的图片目录,但我无法动态获取系统用户名与JavaScript。我需要用户名,因为路径通常是 C:\Users\USERNAME\Pictures。我已经尝试过常量,如 ssfMYPICTURES CSIDL_PROFILE`,但是javascript无法识别它们,或者我错误地使用了它们。



该项目位于node-webkit中,因此使用javascript,node.js或node-webkit API的解决方案应该可行。


Node.js函数在screen_shot.js中:



$ $ p $ exports.getUserHome = function(){
return process.env [(process.platform =='win32')? 'USERPROFILE':'HOME'];
}

index.html中的标题js:

  $(document).ready(function(){
var sc = require('screen_shot');
$('。fileDialog' ).each(function(i){
$(this).attr('nwworkingdir',sc.getUserHome()+'\\Pictures');
});
});

var sc = require('screen_shot');
function wait(e){
var page = $(e).closest('[data-role =page]');
$('#close-popup')。click();
page.find('。fileDialog')。click();


函数screen_shot(fn){
html2canvas($('body'),{
onrendered:function(canvas){
var img split(',')[1];
sc.buildFile(fn,img);
}
});

$ b $('[...]')。live(pagecreate,function(){
$(this).find('。fileDialog ').change(function(){screen_shot($('。fileDialog').val()+'.png');});
});
$ b $('[id ^ =...]')。live(pagecreate,function(){
$(this).find('。fileDialog') ()(){screen_shot($('。fileDialog').val()+'.png');});
});
$ b $('[id ^ =...]')。live(pagecreate,function(){
$(this).find('。fileDialog') ()(){screen_shot($('。fileDialog').val()+'.png');});
});

对话框和按钮:

 < input style =display:none; class =fileDialogtype =filenwworkingdir =nwsaveas /> 
< a href =#onclick =wait(this); data-role =buttondata-theme =j>另存为< / a>


I asked this question a few weeks ago and recently came back to it and solved it. I have a hidden input that looks like this:

                <input style="display:none;" class="fileDialog" type="file" nwworkingdir="C:\Users\" nwsaveas/>

This opens a save dialog at the dir given to nwworkingdir. I would like the default location to be the user's Pictures directory but I am having trouble getting the system user name dynamically with javascript. I need the username because the path is usually C:\Users\USERNAME\Pictures. I have tried constants such asssfMYPICTURESandCSIDL_PROFILE` but javascript doesn't recognize them or I am using them wrong.

The project is in node-webkit so a solution using javascript, node.js, or node-webkit APIs should work.

解决方案

Solution for future readers:

Node.js function in "screen_shot.js":

exports.getUserHome = function() {
    return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
}

header js in index.html:

   $(document).ready(function() {
       var sc= require('screen_shot');
       $('.fileDialog').each(function(i) {
           $(this).attr('nwworkingdir', sc.getUserHome() + '\\Pictures');
       });
   });

                var sc= require('screen_shot');
            function wait(e) {
                var page = $(e).closest('[data-role="page"]');
                $('#close-popup').click();
                page.find('.fileDialog').click();
            }

            function screen_shot(fn) {
                html2canvas($('body'), {
                    onrendered: function(canvas) {
                    var img = canvas.toDataURL("image/png").split(',')[1];
                    sc.buildFile(fn, img);
                  }
                });
            }

            $('["..."]').live("pagecreate", function() {
                $(this).find('.fileDialog').change(function() {screen_shot($('.fileDialog').val() + '.png');});
            });

            $('[id^="..."]').live("pagecreate", function() {
                $(this).find('.fileDialog').change(function() {screen_shot($('.fileDialog').val() + '.png');});
            });

            $('[id^="..."]').live("pagecreate", function() {
                $(this).find('.fileDialog').change(function() {screen_shot($('.fileDialog').val() + '.png');});
            });

dialog and button:

                <input style="display:none;" class="fileDialog" type="file" nwworkingdir="" nwsaveas/>
                <a href="#" onclick="wait(this);" data-role="button" data-theme="j">Save As</a>

这篇关于Windows用户名变量和JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆