does tapestry 5 support vbscript? - vbscript

I've been asked to 'sniff' users' windows username via a vbscript snippet and am having trouble getting this to work in the tapestry (5.1.0.5) application.
It seems tapestry is trying to interpret the vbscript as javascript and therefore failing.
The vbscript snippet (below) is embedded within a component which is in turn loaded conditionally inside a zone as part of a multizoneupdate.
pseudo tml:
<page>
<t:zone>
<t:if>
<t:mycomponent>
<vbscript />
vbscript:
<script type="text/vbscript" language="vbscript">
Dim shell
set shell = createobject("wscript.shell")
set env = shell.environment("process")
set field = document.getElementById("windowsLoginField")
if field is nothing then
alert("no field")
else
field.value = env("username")
end if
</script>
I am aware that this should only work for IE, however other browsers should fail gracefully (not run the script).
When the zone is re-loaded in a state where the vbscript should be rendered, I get the following error in firebug:
missing ; before statement
Dim shell
This is because the script is being evaluated by prototypejs:
evalScripts: function() {
return this.extractScripts().map(function(script) { return eval(script) });
},
Does anyone know of a way to avoid prototype evaluating this script so that it can make it through and be executed as vbscript?
I notice there is no #IncludeVbScriptLibrary annotation ...
thanks, p.

Tapestry inherits this problem from prototype. One solution is to patch the prototype extractScripts and evalScripts so that they do what you want when they see vbscript.
This code works (tested in IE7 and Chrome), but it could be made more flexible (keys off of type and not language for instance)
<script type="text/javascript">
String.prototype.extractScripts = function() {
var matchAll = new RegExp(Prototype.ScriptFragment, 'img');
var matchOne = new RegExp(Prototype.ScriptFragment, 'im');
var matchVBScript = new RegExp('<script.*type=(["\'])text\/vbscript\\1');
return (this.match(matchAll) || []).map(function(scriptTag) {
return [matchVBScript.match(scriptTag), (scriptTag.match(matchOne) || ['', ''])[1]];
});
}
String.prototype.evalScripts = function() {
return this.extractScripts().map(function(script) {
// if it's vbscript and we're in IE then exec it.
if ( script[0] && Prototype.Browser.IE ) return execScript(script[1], "VBScript");
// if it's not vbscript then eval it
if ( !script[0] ) return eval(script[1]);
});
}
</script>

Related

Attempting to make a Timeout for a google script UI alert

I am trying to figure out if I can Auto-Magically select no in a UI alert in google script..
var old = e.oldValue;
var ui = SpreadsheetApp.getUi() ;
if( old == null ){} else{
if( old == "NO"){} else {
if(e.range.getRow() == 3 ) {}
else{ var response = ui.alert('⚠️ Change order? ⚠️', 'Are you sure you want to change this order?',ui.ButtonSet.YES_NO)}}} ;
if(response == ui.Button.NO) {e.range.setValue(old)} ;
this is where I am at, everything works great.... as long as a button is pressed, I am hoping there is a way to Auto press "no" after ~20 seconds.
This is not exactly you want.
Because it doesn't ask for yes or no. It just shows a timeout message. But I think it can be modified as per your requirement.
function showTimeoutAlert(header, message, time){
SpreadsheetApp.getUi().showModelessDialog(
HtmlService.createHtmlOutput(
'<div>'+ message + '</div>' +
'<script> \
var a = document.querySelector("div"); \
a.addEventListener("click",()=>{setTimeout(google.script.host.close,'+ time +')}); \
a.click(); \
</script>'
).setHeight(50),
header
);
}
Use it like:
showTimeoutAlert("No serial number(s) found", "Sr. No(s) 2, 3 not found", 2000)
It will show this message for 2 seconds
One can adjust timeout time in milliseconds.
I believe your goal as follows.
You want to automatically run the script as clicking "NO" button after 20 seconds from opening the dialog.
Modification points:
Unfortunately, in the current stage, the count down cannot be achieved with SpreadsheetApp.getUi().alert(). So in your situation, in order to achieve your goal, I would like to propose to use the custom dialog. When the custom dialog is used, Javascript can be used. By this, the count down can be used.
When above points are reflected to a script, it becomes as follows.
Sample script:
Google Apps Script side:
Please copy and paste the following script to Code.gs of the script editor of Spreadsheet.
function processForNo(value) {
// do something
Browser.msgBox(`Clicked NO. Retrueved value is '${value || ""}'.`);
}
function processForYes(value) {
// do something
Browser.msgBox(`Clicked YES. Retrueved value is '${value || ""}'.`);
}
// Please run this function.
function main() {
var value = "ok";
var html = HtmlService.createTemplateFromFile("index");
html.value = value;
SpreadsheetApp.getUi().showModalDialog(html.evaluate().setHeight(100), '⚠️ Change order? ⚠️');
}
HTML & Javascript:
Please copy and paste the following script to index.html of the script editor of Spreadsheet.
Are you sure you want to change this order?
<input type="button" id="yes" value="YES" onclick="google.script.run.withSuccessHandler(google.script.host.close).processForYes()">
<input type="button" id="no" value="NO" onclick="processForNo('')">
<script>
const processForNo = _ => google.script.run.withSuccessHandler(google.script.host.close).processForNo(<?= value ?>);
setTimeout(processForNo, 20000);
</script>
When main() of above script is run, a dialog is opened. When you don't click the buttons, the dialog is closed after 20 seconds, and the process is run as clicking "NO" button. At that time, processForNo is run. Also, the value can be sent when the dialog is opened.
When "YES" button is clicked, processForYes is run.
Testing:
When above script is tested, the following situation is obtained.
Note:
Above script is a sample script for confirming the script. When above script is included in your script, it becomes as follows.
Google Apps Script side
function myFunction() {
// do something. I cannot understand about your whole script. So please add your actual script and function name.
var old = e.oldValue;
var ui = SpreadsheetApp.getUi();
if (old == null) {
} else {
if (old == "NO") {
} else {
if (e.range.getRow() == 3) {
} else {
var html = HtmlService.createTemplateFromFile("index");
html.value = old;
ui.showModalDialog(html.evaluate().setHeight(100), '⚠️ Change order? ⚠️');
}
}
}
}
function processForNo(old) {
// do something
}
function processForYes(value) {
// do something
}
HTML & Javascript side is the same with above sample script.
Although I cannot confirm your function name from your question, from var old = e.oldValue in your script, I thought that you might be using the OnEdit trigger of the simple trigger. If my understanding is correct, when you use above script, please use the installable OnEdit trigger. By this, when the cell is edited, showModalDialog works. Ref
References:
Custom dialogs
Installable Triggers

Ace modes for XML, JSON work, but not liquid

Setting an ACE editor instance to JSON or XML language mode works great.
But my statement
LiquidMode = ace.require("ace/mode/liquid").Mode,
// fails, ace.require("ace/mode/liquid") returns undefined
Yet the ace/mode/liquid file is defined on the cdn and is returned by it.
Thank you for any ideas or alternatives.
The cdn call and more:
<script src="https://cdn.jsdelivr.net/g/ace#1.2.6(noconflict/ace.js+noconflict/mode-hjson.js+noconflict/mode-liquid.js+noconflict/mode-xml.js+noconflict/theme-chrome.js)">
</script>
// Javascript file
var XMLMode = ace.require("ace/mode/xml").Mode,
JSONMode = ace.require("ace/mode/json").Mode,
LiquidMode = ace.require("ace/mode/liquid").Mode; // fails,
// ace.require("ace/mode/liquid") returns undefined
...
ace_session.setMode(new JSONMode()); // works great
...
ace_session.setMode(new LiquidMode());
When you load ace.js with multiple file syntax, dynamic loading doesn't work, because ace can't determine the url from which it was loaded.
As a workaround you can use
var url = "https://cdn.jsdelivr.net/ace/1.2.6/noconflict/"
ace.config.set("basePath", url)
see https://github.com/ajaxorg/ace/blob/v1.2.6/lib/ace/config.js#L185
Note that you don't need to pass mode object, setMode("ace/mode/liquid") works too.
<script src="https://cdn.jsdelivr.net/g/ace#1.2.6(noconflict/ace.js+noconflict/mode-json.js+noconflict/mode-liquid.js+noconflict/mode-xml.js+noconflict/theme-chrome.js)">
</script>
<script >
// Javascript file
var XMLMode = ace.require("ace/mode/xml").Mode,
JSONMode = ace.require("ace/mode/json").Mode,
LiquidMode = ace.require("ace/mode/liquid").Mode;
debugger
var editor = ace.edit()
var url = "https://cdn.jsdelivr.net/ace/1.2.6/noconflict/";
ace.config.set("basePath", url)
editor.setValue("use core::rand::RngUtil;\n\nfn main() {\n \n}", -1)
editor.setOptions({
autoScrollEditorIntoView: true,
maxLines: 15,
});
document.body.appendChild(editor.container)
editor.session.setMode("ace/mode/rust");
</script>

Skip some tags with stripTags() function in prototypejs

I've successfully implemented a bit of code that strips all HTML from a pasted string using stripTags(). My next goal is to mark a few tags with white flags so they get ignored on 'paste' event using .wrap() to augment the function.
I'm using prototype.js as a framework and have slowly been working through the growing pains of learning both the framework and javascript, but this issue has presented a bit of a roadblock.
I've googled around a bit and found what looks like two great solutions, but I don't seem to be implementing them correctly.
Found solutions:
http://perfectionkills.com/wrap-it-up/ (function to indicate tags to remove)
and
http://pastebin.com/xbymCFi9 (function to allow tags to keep)
I pretty much copied and pasted from the latter.
If I pull the 'br' from the code, then the regex is ignored and all html is stripped. If I leave it, nothing gets pasted.
Here is what I've pieced together (and I feel silly for not being able to figure this out!).
String.prototype.stripTags = String.prototype.stripTags.wrap(
function(proceed, allowTags) {
if (allowTags) {
if (Object.isString(allowTags)) allowTags = $w(allowTags)
this.gsub(/(<\/?\s*)([^\s>]+)(\s[^>]*)?>/, function(match) {
if (allowTags.include(match[2].toLowerCase()))
return match[1] + match[2] + match[3] + '>'
})
} else {
// proceed using the original function
return proceed();
}
});
WysiHat.Commands.promptLinkSelection = function() {
if (this.linkSelected()) {
if (confirm("Remove link?"))
this.unlinkSelection();
} else {
var value = prompt("Enter a URL", "http://www.alltrips.com/");
if (value)
this.linkSelection(value);
}
}
document.on("dom:loaded", function() {
var editor = WysiHat.Editor.attach('event_desc');
var toolbar = new WysiHat.Toolbar(editor);
editor.observe("paste", function(event) {
var el = $(this);
setTimeout(function() {
var pText = el.innerHTML.stripTags('br');
//alert(pText);
$('event_desc_editor').update(pText);
$('event_desc').setValue(pText);
}, 0);
});
(You may recognize the WysiHat code from 37Signals text editor)
note: you can see the alert commented out. If I do alert the ptext, I get 'undefined' returned.
So I've given up on and moved to a regex solution:
el.innerHTML.replace(/<(?!\s*\/?\s*p\b)[^>]*>/gi,'')

How to execute a page ,that contains JS ,in AJAX ,using innerHTML?

I send GET data with AJAX to another file.And on the another file I have echo "<script>alert('Something');</script>";.This is displayed dynamicly with AJAX ,i.e
var ajaxDisplay = document.getElementById('edit');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
puts the <script>alert('Something');</script> to div with name edit.
But it doesn't alert anything.
How to get it work?
I have mixed html/javascript.
Here is the code.
function ajaxFunctions(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('edit');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var namef = document.getElementById('nameed').value;
var queryString = "?namef=" + namef;
ajaxRequest.open("GET", "try.php" + queryString, true);
ajaxRequest.send(null);
}
Maybe to find the script tags and to eval them?
But how to find the script tags?
Instead of trying to inject a script element in the DOM, just have your script return:
alert('Something');
And then use eval(response); to run it. Or you could add a script element with the src attribute pointing to the page that returns your JavaScript into the <head> (which is the preferred method).
function loadScript(url) {
var head = document.getElementsByTagName("head")[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
head.appendChild(script);
}
Keep in mind that this wont work for cross-domain requests--the script has to have the same origin as the page running your code. To get around this, you'll have to use a callback.
It looks like the only purpose of setting innerHTML is an attempt to get the JS to execute. But once the page is loaded, JS won't 'know' that it needs to parse and execute the new text you've changed, so your method won't work. In this case, what you want is a callback function:
http://api.jquery.com/jQuery.ajax/
I haven't used jQuery, but it looks like you'd simply add a 'complete' property to the settings object you pass to the .ajax() call, like so:
$.ajax({
// ......
complete: function(){
alert('Something');
}
// ......
});
In this case, the callback function would execute once the ajax call has completed. You can pick other events, such as on success, on failure, and so on, if you need to attach your code to a different event.
But how to find the script tags?
Well, parent.getElementsByTagName('script') and then evaling the data of the text node inside will do it.
However, inserting content that includes script tags is unreliable and works slightly differently across browsers. eg. IE will execute the script the first time the script node is inserted into any parent, inside the document or not, whilst Firefox will execute script the first time a subtree including the script is added to a node inside the document. So if you're not extremely careful, you can end up executing scripts twice on some browsers, or executing the script at the wrong time, following a further page manipulation.
So don't. Return script that you want to execute separately to any HTML content, eg. using a JSON object containing both the HTML and the JavaScript seperately.

Can I initialize objects written in JScript from VBScript?

I am trying to write a WSH logon script. Administrators throughout the company need to be able to customize the execution of the script, and execute additional scripts, for specific locations and users. In order to make their jobs easier, I would like to provide an API that the administrators can access in their scripts. If I write my API using JScript, would it be possible to initialize the objects I define through VBScript? For example, consider the following code:
<!-- The WSF logon script file -->
<package>
<job>
<script language="JScript">
// A demonstration function
function OverNineThousand() {
return 9001;
}
// A demonstration "class"
function WorkstationClass() {
var os = "Windows XP";
this.getOperatingSystem = function() {
return os;
}
}
</script>
<script language="VBScript">
Dim bigNumber, workstation
'// This assignment works properly.
bigNumber = OverNineThousand()
'// This assignment causes an error. Am I doing it wrong?
Set workstation = New WorkstationClass()
'// Execution never gets this far
WScript.Echo workstation.getOperatingSystem()
</script>
</job>
</package>
Is there any way to accomplish what I'm trying to do?
VBScript and JScript seem to disagree on how to initialize an object. However, once the object has been initialized it is recognized by both languages. To get around this I had to create the object in JScript and then return it to the VBScript caller, as demonstrated below.
<package>
<job>
<script language="JScript">
// A demonstration "class"
function WorkstationClass() {
var os = "Windows XP";
this.getOperatingSystem = function() {
return os;
}
}
function CreateWorkstation() {
return new WorkstationClass();
}
</script>
<script language="VBScript">
Dim workstation
'// This assignment causes an error.
'// Set workstation = New WorkstationClass()
'// This works!
Set workstation = CreateWorkstation()
'// Prints "Windows XP"
WScript.Echo workstation.getOperatingSystem()
</script>
</job>
</package>

Resources