Skip to content

Instantly share code, notes, and snippets.

@impepc
Created April 26, 2018 11:10
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 impepc/ddcf4f7d96e580ffdbeed1b5b9ed5375 to your computer and use it in GitHub Desktop.
Save impepc/ddcf4f7d96e580ffdbeed1b5b9ed5375 to your computer and use it in GitHub Desktop.
fileviewer wsh
var fso = new ActiveXObject("Scripting.FileSystemObject");
var ie = WScript.CreateObject("InternetExplorer.Application", "IE_");
var objshell = new ActiveXObject("shell.application");
var shell = WScript.CreateObject("WScript.Shell");
/* store current folder item's path */
var info = [];
/* position of showing image */
var position = 0;
init();
fileOpen(fso.GetAbsolutePathName(WScript.arguments.Item(0)));
focus();
while(true) {
WScript.Sleep(1000);
}
// create page image
function init() {
ie.Visible = true;
ie.Navigate("about:blank");
var img = ie.Document.createElement("img");
var text = ie.Document.createElement("div");
var title = ie.Document.createElement("div");
var video = ie.Document.createElement("video");
ie.Document.body.appendChild(title);
title.id = "title";
title.style.cssText = "width:100%; text-align:center; display:block";
ie.Document.body.appendChild(text);
text.id = "text";
text.style.cssText = "border: 1px solid black;";
text.addEventListener("mouseDown", openByApp);
ie.Document.body.appendChild(img);
img.id = "img";
img.style.cssText = "display:block; margin:auto ";
img.addEventListener("mouseDown", openByApp);
ie.Document.body.appendChild(video);
video.id = "video";
video.style.cssText = "margin: auto";
/* set actions of keydown */
ie.Document.addEventListener("keydown", function(e) {
// left allow
if (e.keyCode == 37) {
position = position - 1 >= 0 ? position - 1 : info.length - 1;
show(info[position]);
// up allow
} else if (e.keyCode == 38) {
fileOpen(fso.GetParentFolderName(info[position]['path']));
// right allow
} else if (e.keyCode == 39) {
position = position + 1 < info.length ? position + 1 : 0;
show(info[position]);
// down allow
} else if (e.keyCode == 40) {
if ( info[position]['type'] == 'folder' ) {
folderOpen(info[position]['path']);
}
// space key
} else if (e.keyCode == 32) {
openByApp();
}
});
}
function focus() {
var ie_list = [];
/* get root IE process and focus it */
for ( var e = new Enumerator(GetObject("winmgmts:").InstancesOf("Win32_Process"));
!e.atEnd(); e.moveNext()) {
var p = e.item();
/* app activate and creation date of ie which recently activate */
if ( p.Name == "iexplore.exe") {
ie_list.push( {
'pid': p.ProcessId,
'ppid': p.ParentProcessId
});
}
}
var process = ie_list[0];
for ( var i = 0; i < ie_list.length; i++) {
if ( process['ppid'] == ie_list['pid'] ) {
i = 0;
}
}
shell.AppActivate(process['pid']);
ie.Document.focus();
}
function folderOpen(target) {
setInfo(target);
setPosition(info[0]['path']);
show(info[position]);
}
function fileOpen(target) {
setInfo(fso.GetParentFolderName(target));
setPosition(target);
show(info[position]);
}
function setInfo(target) {
info = getItems("folder", fso.GetFolder(target).SubFolders);
var files = getItems("file", fso.GetFolder(target).Files);
for ( var i = 0; i < files.length; i++) {
info.push(files[i]);
}
}
function getItems(type, list) {
var ret = [];
for ( var e = new Enumerator(list); !e.atEnd(); e.moveNext()) {
ret.push({'type': type, 'path':e.item().Path});
}
return ret;
}
function setPosition(path) {
for (var i = 0; i < info.length; i++) {
if (info[i]['path'] == path) {
position = i;
}
}
}
/* procedure show item */
function show(item) {
var path = item['path'];
var img = ie.Document.getElementById('img');
var title = ie.Document.getElementById('title');
var text = ie.Document.getElementById('text');
var video = ie.Document.getElementById('video');
/* at first, undisplay all */
text.style.display = "none";
img.style.display = "none";
video.style.display = "none";
title.innerText = path;
if ( item['type'] == 'folder') {
text.innerText = "[Folder]";
text.style.display = "block";
} else {
if ( fso.GetExtensionName(path).match(/(png|jpg|jpeg|gif|bmp|heic|heif|jp2|j2c|webp|tiff|tif|svg|svgz)/i)) {
img.src = path;
resize(img, title);
img.style.display = "block";
} else if (fso.GetExtensionName(path).match(/mp4|webm|ogg/i)) {
video.src = path;
resize(video, title);
video.style.display = "block";
} else if (fso.GetExtensionName(path).match(/txt/i)) {
var fh;
try {
fh = fso.OpenTextFile(path);
text.innerText = fh.ReadAll();
text.style.display = "block";;
} finally {
if ( fh != null) { fh.Close(); }
}
} else {
text.innerText = "[unablable to display]";
text.style.display = "block";
}
}
}
function openByApp(e) {
var path = info[position]['path'];
if ( fso.GetExtensionName(path).match(/(png|jpg|jpeg|gif|bmp|heic|heif|jp2|j2c|webp|tiff|tif|svg|svgz)/i)) {
shell.run("mspaint.exe \"" + info[position]['path'] + "\"");
} else if (fso.GetExtensionName(path).match(/txt/i)) {
shell.run("notepad.exe \"" + info[position]['path'] + "\"");
}
}
/* resize e(lement) size to fix window */
function resize(e, title) {
if (e.naturalWidth < ie.Document.body.clientWidth && e.naturalHeight < ie.Document.body.clientHeight) {
e.style.height = e.naturalHeight;
e.style.width = e.naturalWidth;
} else {
var height_ratio = (ie.Document.body.clientHeight - title.clientHeight - title.offsetTop * 2 - 2) / e.naturalHeight;
var width_ratio = ie.Document.body.clientWidth / e.naturalWidth;
if ( height_ratio < width_ratio) {
e.style.height = e.naturalHeight * height_ratio;
e.style.width = e.naturalWidth * height_ratio;
} else {
e.style.height = e.naturalHeight * width_ratio;
e.style.width = e.naturalWidth * width_ratio;
}
}
e.style.display = "block";
}
/* Quit self, when IE quit */
function IE_OnQuit() {
WScript.Quit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment