work with folders using activexobject - javascript

I need to create folder,copy it and delete.
So I've created several javascript functions - to get path to the folder, delete folder,create and copy folder.
But when I try to run it in google chrome I've got an exception
I thought it was problem with ActiveX. But in IE it doesn't work either.
<html>
<head>
<meta charset="utf-8">
<script type="text/javasript">
function onFolder(){
var ob = new ActiveXObject("Scripting.FileSystemObject");
var name = document.getElementById("idtextbox1").value;
var path = document.getElementById("idtextbox2").value;
var x = path + "\\" + name;
return x;
}
function onCreate(){
var ob = new ActiveXObject("Scripting.FileSystemObject");
var path = onFolder();
var x = ob.CreateFolder(path);
}
function onCopy(){
var ob = new ActiveXObject("Scripting.FileSystemObject");
var source = onFolder();
var dest = document.getElementById("idtextbox3").value;
ob.CopyFolder(source,dest + "\\");
}
function onDelete(){
var ob = new ActiveXObject("Scripting.FileSystemObject");
var folder = onFolder();
ob.DeleteFolder(folder);
}
</script>
</head>
<body>
Folder name<input type="text" id="idtextbox1">
<br>
Destination<input type="text" id="idtextbox2">
<br>
Copy destination<input type="text" id="idtextbox3">
<br>
<input type="button" id="idbutton1" value="Create" onClick="onCreate()">
<input type="button" id="idbutton2" value="Copy" onClick="onCopy()">
<input type="button" id="idbutton3" value="Delete" onClick="onDelete()">
</body>
</html>
What's the problem? Is it ActiveX? Because I think javascript functions are correct.

The big problem is that you are using chrome Chrome. ActiveX only works in IE because it is a non-standard function. And for what you are trying to do, the security level has to be on low which is very unconventional. JavaScript is just not a good language to be trying to access or update a computers file system.

Related

How do I get the value of id="text1" to be added at the end of the URL?

I am more the hardware guy and my programming skills really suck. I am trying to create things mostly by trial and error and the help of google. I am helping out some GFX-/Ad-Designers, who basically create stuff, place it on a website and have to run those websites thru severals browsers. I am trying to make this less manually handed.
This is run in a HTA. As I said, I am not a programming guy and this was something I could easly work with =/ probably some other language could do this by ease...but as said...
tl;dr
How do I get the value of id="text1" to be added at the end of the URL
shell.run("Firefox https://www.example.com=(text1.value)"); doesnt work.
I does work if I manually change the URL, but than I would not have a handy input-field and changing the URLs by hand...I guess the ad-creating ppl will mess up things.
So, thats what I have done so far...but I can't fix it.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Sub-Site Automation</title>
<hta:application applicationname="Run test on browsers" scroll="yes" singleinstance="yes">
<script type="text/javascript">
function openURL()
{
var input = document.getElementById("text1");
/* console.log(input); // Log
*/
var inputValue = input.value;
/* console.log(inputValue); // Log2
*/
var shell = new ActiveXObject("WScript.Shell");
shell.run("Firefox https://www.example.com=(text1.value)");
shell.run("Chrome https://www.example.com=(text1.value)");
shell.run("file:///C:\Users\*\AppData\Local\Vivaldi\Application\vivaldi.exe https://www.example.com=(text1.value)");
}
</script>
</head>
<body>
<input type="text" id="text1" Name="text1" value="Place ID of subwebsite here"><br>
<input type="submit" Value="Open in all Webbrowsers" onclick="openURL()">
</body>
</html>
Please help!
As I mentioned in the comments, the answer is in your code itself.
See the lines:
// This line gets the element you want
var input = document.getElementById("text1");
// This line gets it's value. You need this value
var inputValue = input.value;
The variable inputValue is what you need to replace there instead of using text1.value.
So your function would be as follows:
function openURL() {
var input = document.getElementById("text1");
var inputValue = input.value;
var shell = new ActiveXObject("WScript.Shell");
shell.run("Firefox https://www.example.com=("+inputValue+")");
shell.run("Chrome https://www.example.com=("+inputValue+")");
shell.run("file:///C:\Users\*\AppData\Local\Vivaldi\Application\vivaldi.exe https://www.example.com=("+inputValue+")");
}
the problem ist that you have no reference to the elements with id text1 so you can not access its value. A second problem is that you try to access a variable inside of a string literal which could be solved in es5 with string concatenation or in newer ecmascript versions with template literals.
Depending on the input value you should also use encodeURIComponent so that the resulting URL is valid.
One version that would work is the following:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Sub-Site Automation</title>
<hta:application applicationname="Run test on browsers" scroll="yes" singleinstance="yes">
</head>
<body>
<input type="text" id="text1" Name="text1" value="Place ID of subwebsite here"><br>
<input type="submit" Value="Open in all Webbrowsers" onclick="openURL()">
<script type="text/javascript">
function openURL()
{
var text1 = document.getElementById("text1");
var shell = new ActiveXObject("WScript.Shell");
shell.run("Firefox https://www.example.com=" + text1.value);
shell.run("Chrome https://www.example.com=" + text1.value);
shell.run("file:///C:\Users\*\AppData\Local\Vivaldi\Application\vivaldi.exe https://www.example.com=" + text1.value);
}
</script>
</body>
</html>

Uncaught Error: Module name "antlr4/index" has not been loaded yet for context on require.js

I try to use antlr4 on javascript, then read https://tomassetti.me/antlr-and-the-web/ and make but error has occurred.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="lib/require.js"></script>
<script type="text/javascript">
var antlr4 = require('antlr4/index');
var QueryLexer = require('gram/queryLexer');
var QueryParser = require('gram/queryParser');
document.getElementById("parse").addEventListener("click", function() {
var input = document.getElementById("code").value;
var chars = new antlr4.InputStream(input);
var lexer = new QueryLexer.queryLexer(chars);
var tokens = new antlr4.CommonTokenStream(lexer);
var parser = new QueryParser.queryParser(tokens);
parser.buildParseTrees = true;
var tree = parser.query();
console.log("Parsed: "+ tree);
});
</script>
</head>
<body>
<div id="inputs">
<textarea id="code">
* play with antlr4
* write a tutorial
</textarea>
<br/>
<button id="parse">Parse</button>
</div>
</body>
</html>
The error may cause on "var antlr4 = require('antlr4/index');".
I downloaded antlr4 from http://www.antlr.org/download/index.html and put same tier of "index.html". In lib directory there exist "require.js".
index.js
exports.atn = require('./atn/index');
exports.codepointat = require('./polyfills/codepointat');
exports.dfa = require('./dfa/index');
exports.fromcodepoint = require('./polyfills/fromcodepoint');
exports.tree = require('./tree/index');
exports.error = require('./error/index');
exports.Token = require('./Token').Token;
exports.CharStreams = require('./CharStreams').CharStreams;
exports.CommonToken = require('./Token').CommonToken;
exports.InputStream = require('./InputStream').InputStream;
exports.FileStream = require('./FileStream').FileStream;
exports.CommonTokenStream = require('./CommonTokenStream').CommonTokenStream;
exports.Lexer = require('./Lexer').Lexer;
exports.Parser = require('./Parser').Parser;
var pc = require('./PredictionContext');
exports.PredictionContextCache = pc.PredictionContextCache;
exports.ParserRuleContext = require('./ParserRuleContext').ParserRuleContext;
exports.Interval = require('./IntervalSet').Interval;
exports.Utils = require('./Utils');
I think there are no problems, because require path('antlr4/index') is not wrong.
But error has occurred. Please give me some idea.
The code you show in your question cannot work as-is with RequireJS. You'd have to write the require calls differently, or wrap all the require calls you have in a define so as to use the CommonJS support that RequireJS provides.
But the tutorial is not asking you to use RequireJS. if you go to the github repo that the writer of the tutorial provided, you'll see:
Require.js was obtained from https://github.com/letorbi/smoothie/blob/master/standalone/require.js
You have to use that file, which is not RequireJS, but something similar to it in the sense that it also loads scripts, and yet different from RequireJS in the sense that it seems to support the CommonJS module format as-is, which RequireJS doesn't.

How to get full path name of uploaded folder using javascript? [duplicate]

This question already has answers here:
How to resolve the C:\fakepath?
(13 answers)
Closed 5 years ago.
<!DOCTYPE html>
<html>
<body>
<div align="center" style="width:800px" >
LocalShare:
<input type="file" id="FileUpload" onchange="selectFolder(event)" webkitdirectory mozdirectory multiple />
<p id="demo"></p>
<p id="demo1"></p>
<script>
function selectFolder(e) {
var txt = "";
var theFiles = e.target.files;
var relativePath = theFiles[0].webkitdirectory;
var relativePath = theFiles[0].webkitRelativePath;
var x = document.getElementById("FileUpload").value;
document.getElementById("demo").innerHTML = x;
document.getElementById("demo1").innerHTML = relativePath;
var folder = relativePath.split("/");
alert(folder[0]);
}
</script>
</body>
</html>
Output of the above code is:
C:\fakepath\filename
foldername
Here it is displaying folder name and path name. It is taking default value as "fakepath" but I need to find out exact path!
Well, Earlier it was allowed but due to security reason browser doesn't allow us to get client machine's internal details.

Javascript changing values inside a iframe

I have my website
www.aplicatii-iphone.ro
and another
page.html on localhost
<html>
<head>
<title>Object References across Iframes</title>
<script type="text/javascript">
window.onload = function(){
var form = document.getElementById('testForm');
form.testBtn.onclick = sendData;
}
function notify() {
//alert('iframe loaded');
var iframeEl = document.getElementById('ifrm');
if ( iframeEl && iframeEl.parentNode && document.createElement ) {
var newTxt = document.createTextNode('The iframe has loaded and your browser supports it\'s onload attribute.');
var newPara = document.createElement("p");
newPara.className = 'demo';
newPara.appendChild(newTxt);
iframeEl.parentNode.insertBefore(newPara, iframeEl);
}
}
function sendData() { // to form inside iframed document
// frames array method:
// window.frames['ifrm'].document.forms['ifrmTest'].elements['display'].value = this.form.testEntry.value;
var ifrm = document.getElementById('ifrm');
var doc = ifrm.contentDocument? ifrm.contentDocument: ifrm.contentWindow.document;
var form = doc.getElementById('search-input'); // <------<< search input
form.display.value = this.form.testEntry.value;
form.submit();
}
// test in iframed doc
var counter = 0;
</script>
</head>
<body>
<form id="testForm" action="#">
<p><input type="text" name="testEntry" size="30" value="[enter something]" /> <input name="testBtn" type="button" value="Click Me" /></p>
</form>
<iframe name="ifrm" id="ifrm" src="http://www.aplicatii-iphone.ro" onload="notify()" width="900">Sorry, your browser doesn't support iframes.</iframe>
</body>
</html>
And every time I press the button Click Me, I want that the state of www.aplicatii-iphone.ro to be like a user searched for that value written in "testEntry" from outside of the iframe.
I tried something there ... but I can't figure it out ... any help please?
I took the example from here http://www.dyn-web.com/tutorials/iframes/refs.php
If you know you're using a modern browser, you could use postMessage to communicate between the frames. Here's a good write-up: http://ajaxian.com/archives/cross-window-messaging-with-html-5-postmessage
If you need to support legacy browsers, you could use Google Closure's CrossPageChannel object to communicate between frames.
Unfortunatly, this is not possible due to the Same orgin policy.
And changing the document.domain-value only helps if you try to connect a subdomain with the main-domain.
Edit
If you avoid the same-orgin-problem by using a page on the same website, this should work for you:
window.frames['ifrm'].document.getElementById("search-input").value = document.getElementsByName("testEntry")[0].value;
window.frames['ifrm'].document.getElementById("cse-search-box").submit();

simple xml editor using javascript and msxml

whether there is an example of such an implementation?
howto load/save context of msxml to/from edit or text control?
<html><head>
<script language="JavaScript">
function loadxml()
{
var fileName = document.getElementById("fileName");
var xmlData = new ActiveXObject("Msxml2.DOMDocument");
xmlData.load(fileName.value);
var editor = document.getElementById("editor");
editor.value = xmlData; // got [object]
}
function testxml()
{
var editor = document.getElementById("editor");
// editor.value load by msxml
}
</script>
</head>
<body>
<input type="file" id="fileName"/>
<input type="button" value="Load" onclick="loadxml();"/>
<input type="button" value="Test" onclick="testxml();"/><br>
<textarea id="editor" rows="25" cols="50">no data</textarea>
</body>
</html>
editor.value = xmlData.xml
And vice-versa only use loadXML instead of load method on ActiveX object
var xmlData = new ActiveXObject("Msxml2.DOMDocument");
xmlData.loadXML( editor.value );
see for ex. http://joncom.be/code/javascript-xml-conversion/
PS Originally misunderstood question, so there are irrelevant comments now

Categories