How do you get the selected listBox item as a String? - listbox

I have been trying to build a small app that takes the selected item from a listBox and appends it to the filename of a file that will be uploaded. I can display everything correctly, and the file gets uploaded, but the value that should be appended to the filename is 'undefined'. Here is a sample:
function doGet(e) {
var app = UiApp.createApplication().setTitle("Upload CSV to Sheet");
var panel = app.createVerticalPanel();
var listBox = app.createListBox().setName('myList').setId('myList').setWidth('80px');
listBox.addItem('Value1');
listBox.addItem('Value2');
listBox.addItem('Value3');
listBox.addItem('Value4');
panel.add(listBox);
app.add(panel);
var formContent = app.createVerticalPanel();
formContent.add(app.createFileUpload().setName('thefile'));
formContent.add(app.createSubmitButton('Submit'));
var form = app.createFormPanel();
form.add(formContent);
app.add(form);
return app;
}
function doPost(e) {
var fileBlob = e.parameter.thefile;
var app = UiApp.getActiveApplication();
var doc = DocsList.createFile(fileBlob).rename(e.parameter.myList+Utilities.formatDate(new Date(), "GMT", "MM-dd-yy"));
return app;
}
How can I get the name of the file to take the value from the listBox?

You added the listBox to the app instead of adding it to the formpanel content.
This has 2 consequences :
1 - the list stays visible after submitting
2 - the list is not received by the doPost since it is not in the form...
Try it like this :
function doGet() {
var app = UiApp.createApplication().setTitle("Upload CSV to Sheet");
var panel = app.createVerticalPanel();
var listBox = app.createListBox().setName('myList').setId('myList').setWidth('80px');
listBox.addItem('Value1');
listBox.addItem('Value2');
listBox.addItem('Value3');
listBox.addItem('Value4');
panel.add(listBox);
var formContent = app.createVerticalPanel();
formContent.add(app.createFileUpload().setName('thefile'));
formContent.add(app.createSubmitButton('Submit'));
formContent.add(panel);
var form = app.createFormPanel();
form.add(formContent);
app.add(form);
return app;
}
function doPost(e) {
var fileBlob = e.parameter.thefile;
var doc = DocsList.createFile(fileBlob).rename(e.parameter.myList+' '+Utilities.formatDate(new Date(), "GMT", "MM-dd-yy"));
var app = UiApp.getActiveApplication();
return app;
}

Related

Sharing same google form across multiple spreadsheets

I have created a Feedback form and want to open it from and share it across multiple spreadsheets. I have used a revised version of the code in the following post Single Google Form for multiple Sheets.
The form opens as expected as an IFRAME but does not allow me (or any user) to populate the fields and create a response. What am I missing?
function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('Data Architecture')
.addItem('Create File Note', 'menuItem1')
.addSeparator()
.addSubMenu(ui.createMenu('Feedback & Actions')
.addItem('Provide Feedback', 'provideFeedback')
.addItem('Create and Action', 'createAction'))
.addToUi();
}
function provideFeedback() {
var documentName = SpreadsheetApp.getActive().getName();
var dataentityName = SpreadsheetApp.getActiveRange().getCell(1, 1).getDisplayValue();
launchForm();
}
function createAction() {
var documentName = SpreadsheetApp.getActive().getName()
var documentID = SpreadsheetApp.getActive().getId()
var dataentityName = SpreadsheetApp.getActiveRange().getCell(1, 1).getDisplayValue()
launchForm();
}
function launchForm(){
var formID='1pmH3AWiMUczat5uIaZ5zaT--cmDjq9v3W9pePPjwGF0';
var form = FormApp.openById(formID);
var formURL = form.getPublishedUrl();
var response = UrlFetchApp.fetch(formURL);
var formHtml = response.getContentText();
var htmlApp = HtmlService
.createHtmlOutput(formHtml)
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('Feedback and action form')
.setWidth(800)
.setHeight(800);
SpreadsheetApp.getActiveSpreadsheet().show(htmlApp);
}
Instead of accessing the web page and generating the HTML content and serve it again, you can use iframe tag with src to your URL to do it for you.
function launchForm(){
var formID='1pmH3AWiMUczat5uIaZ5zaT--cmDjq9v3W9pePPjwGF0';
var form = FormApp.openById(formID);
var formURL = form.getPublishedUrl()+ "?embedded=true"
var htmlApp = HtmlService
.createHtmlOutput('<iframe src="'+formURL+'" width="500" height="400" frameborder="0" marginheight="0" marginwidth="0"> </iframe>')
.setTitle('Feedback and action form')
.setWidth(400)
.setHeight(400);
SpreadsheetApp.getActiveSpreadsheet().show(htmlApp);
}
To have pre-filled form to be used in your script:
1) Get a prefilled Form, by following the steps here
2) For a form like this:
the prefilled url will look like this:
var formURL = "https://docs.google.com/forms/d/e/{Publish ID}/viewform?
usp=pp_url&
entry.1739076204=Jack+Brown&
entry.1869878870=2017-05-12&
entry.799903038=Incorrect&
entry.1996064114=I+am+the+best&
entry.607023772=Select+Me"
I have separated out the individual options so that it is easier to compare with an entry in the form. Now to modify the entry all you have to do is modify the value after '=' sign for each entry. So to change the name you would do:
var name = "Chuck Norris"
entry.1739076204='+name+'
Similarly, you can modify each enter by modifying the URL. The below code modifies the entry for a specific form I have hosted. You will have to modify if as per your needs
function launchForm(){
var publishedURl = "https://docs.google.com/forms/d/e/1FAIpQLSdsHM53jTWJ0Eqn8VSxE5bWHjnD9KXsVBrqLsBwtyJIIsjpnA/viewform"
var name = "Chuck Not Norris"
var dt = Utilities.formatDate(new Date(), "GMT-05:00", "yyyy-MM-dd")
var options1 = ["Correct","Incorrect"]
var options2 = "Chuck Norris is google Apps Scripts"
var options3 = ["Select Me","Don't select me"]
var preEditUrl = publishedURl + '?usp=pp_url&entry.1739076204='+name
+'&entry.1869878870='+dt
+'&entry.799903038='+options1[0]
+'&entry.1996064114='+options2
+'&entry.607023772='+options3[0]
var htmlApp = HtmlService
.createHtmlOutput('<iframe src="'+preEditUrl+'" width="500" height="400" frameborder="0" marginheight="0" marginwidth="0"> </iframe>')
.setTitle('Feedback and action form')
.setWidth(400)
.setHeight(400);
SpreadsheetApp.getActiveSpreadsheet().show(htmlApp);
}
Hopefully, this will get you started in the right direction.

Get text from textarea

I feel like I am going insane trying to get the most simple of things to work! I have a mailing list in Google Spreadsheet on Google Drive. I have created a user interface that prompts for the message that the user wants to include in the email. My code looks like this.
function startBulkEmailUI(e) {
//create main form
var app = UiApp.createApplication().setTitle('Send Email').setWidth(602).setHeight(402);
var scrollPanel = app.createScrollPanel().setId('scrollPanel').setAlwaysShowScrollBars(true) ;
var mainPanel = app.createVerticalPanel().setId('mainPanel');
mainPanel.setStyleAttribute('border', '1px solid #C0C0C0').setWidth("100%").setHeight("100%");
scrollPanel.add(mainPanel);
//create panel for input boxes etc
var inputPanel = app.createVerticalPanel().setId('inputPanel').setWidth("100%").setHeight("100%").setSpacing(0);
var messageLabel = app.createLabel('E-mail Message');
var messageBox = app.createTextArea().setName('messageBox').setId('messageBox').setVisible(true).setWidth(400).setHeight(150);
var attachmentLabel = app.createLabel('E-mail Attachment');
var attachmentCheckBox = app.createCheckBox('Attach a file').setValue(false);
inputPanel.add(messageLabel).add(messageBox).add(attachmentLabel).add(attachmentCheckBox);
mainPanel.add(inputPanel).setCellHorizontalAlignment(inputPanel, UiApp.HorizontalAlignment.CENTER) ;
inputPanel.setCellHorizontalAlignment(messageLabel, UiApp.HorizontalAlignment.CENTER);
inputPanel.setCellHorizontalAlignment(messageBox, UiApp.HorizontalAlignment.CENTER);
inputPanel.setCellHorizontalAlignment(attachmentLabel, UiApp.HorizontalAlignment.CENTER);
inputPanel.setCellHorizontalAlignment(attachmentCheckBox, UiApp.HorizontalAlignment.CENTER);
//add function buttons
var buttonsPanel = app.createHorizontalPanel().setStyleAttribute('margin', '20px').setWidth(500);
var closeButton = app.createButton('Close',app.createServerHandler('close_'));
var sendButton = app.createButton('Send!').setId("sendButton");
buttonsPanel.setId('buttonsPanel').add(closeButton).add(sendButton);
mainPanel.add(buttonsPanel).setCellHorizontalAlignment(buttonsPanel, UiApp.HorizontalAlignment.CENTER);
buttonsPanel.setCellHorizontalAlignment(closeButton, UiApp.HorizontalAlignment.CENTER).setCellHorizontalAlignment(sendButton, UiApp.HorizontalAlignment.CENTER);
app.add(scrollPanel);
//handlers
var handlerSendEmails = app.createServerClickHandler('sendEmails');
handlerSendEmails.addCallbackElement(inputPanel);
sendButton.addClickHandler(handlerSendEmails);
//show the form
ss.show(app);
return app;
}
function sendEmails(e) {
var contactDetail = getContactDetail();
var count = 0;
var failed = 0;
for (i in contactDetail) {
var row = contactDetail[i];
var emailAddress = row[9];
var stuff = e.parameter.messageBox.value;
Logger.log(e.parameter);
var message = 'Hi, ' + row[4] + '\n\n' + 'This is a test!'; // Second column
var subject = 'Test Email';
try {
// MailApp.sendEmail(emailAddress, subject, message);
count = count+1;
} catch(e) {
failed = failed+1;
}
}
var msg = stuff;
Browser.msgBox('Debug Msg', msg, Browser.Buttons.OK);
}
I want the user to enter information into the textarea, which I can then use as the body of an email. I discovered there is no getText, so I have found hundreds of answer saying it should look like the code above: you create a callbackhandler, then use e.parameter.OBJECT_NAME.value to get the text. It simply does not work for me and I have tried every workaround that I can think of. Any suggestions? Is my code faulty (I know it's messy...)?
The "normal" way to get a widget's value in a handler function is in the form e.parameter.widgetName so in you case you should simply use
var stuff = e.parameter.messageBox; without the ".value" , the result is a string.

Google Script - Can't combine on the same script multiple selection from listBox and SubmitButton

I'm trying to code on google app and I've encountered one issue. For example, let's consider this example code on google website ( link's here https://developers.google.com/apps-script/class_formpanel )
function doGet() {
var app = UiApp.createApplication();
var form = app.createFormPanel();
var flow = app.createFlowPanel();
flow.add(app.createTextBox().setName("textBox"));
flow.add(app.createListBox().setName("listBox").addItem("option 1").addItem("option 2"));
flow.add(app.createSubmitButton("Submit"));
form.add(flow);
app.add(form);
return app;
}
function doPost(eventInfo) {
var app = UiApp.getActiveApplication();
app.add(app.createLabel("Form submitted. The text box's value was '" +
eventInfo.parameter.textBox +
"' and the list box's value was '" +
eventInfo.parameter.listBox + "'"));
return app;
}
My issue is that I want to select multiple values on the listbox. I change then line 6 in
flow.add(app.createListBox(true).setName("listBox").addItem("option 1").addItem("option 2"));
to allow multiple selection. But the result is that only the last selected value is taken, preventing multiple selections. Apparently, it is due to the submitButton. I need to keep the formPanel because on a further code I'll like to combine uploading files and listBox multiple selection. How may I fix that? Thank you a lot
As a complement to Mogsdad's answer, note that this bug / issue is only concerning the doPost structured handler... if you don't need the file upload feature you could use a simple doGet + handler with callbackElement and in this case the multiselect list is available and works as expected.
test function :
function doGet() {
var app = UiApp.createApplication().setTitle('test listBox');
var panel = app.createHorizontalPanel().setStyleAttribute('padding','40px');
var sHdlr = app.createServerHandler('validateList').addCallbackElement(panel);
var items = ['category 1','category 2','category 3'];
var list1 = app.createListBox(true).setName('list1');
for(var i =0;i<items.length;++i){list1.addItem(items[i],i)}
panel.add(list1).add(app.createButton('validate',sHdlr));
app.add(panel);
return app;
}
function validateList(e){
var app = UiApp.getActiveApplication();
app.add(app.createLabel("Value(s) in list : "+e.parameter.list1).setStyleAttribute('margin-left','40'));
return app;
}
and below is a working example of the workaround described in the issue tracker.
I used a textBox to show the process, set it to visible(false) or use a hidden widget in a 'real' app. ( Test available here )
function doGet() {
var app = UiApp.createApplication().setTitle('test listBox');
var panel = app.createHorizontalPanel().setStyleAttribute('padding','40px');
var listHandler = app.createServerHandler('updlistVal').addCallbackElement(panel);
var items = ['category 1','category 2','category 3'];
var list1 = app.createListBox(true).setName('list1').addChangeHandler(listHandler);
for(var i =0;i<items.length;++i){list1.addItem(items[i])}
panel.add(list1).add(app.createTextBox().setText(items[0]).setId('listboxVal').setName('listboxVal').setWidth('200'));// set a default value in case the user is happy with that and doesn't touch the listBox
var submitBtn = app.createSubmitButton('test').setStyleAttribute('margin-left','40');
app.add(app.createFormPanel().add(panel.add(submitBtn)));
return app;
}
function doPost(e){
var app = UiApp.getActiveApplication();
app.add(app.createLabel().setStyleAttribute('padding','40').setText("Submitted value(s) from list : "+e.parameter.listboxVal));
return app;
}
function updlistVal(e){
var app = UiApp.getActiveApplication();
app.getElementById('listboxVal').setValue(e.parameter.list1);
return app;
}
EDIT 2 :
As Mentioned in the comments on this post we must find a way to prevent going through the submission before the value of the hidden/text widget has been updated with a valid value. The "default value" I used above is a possible solution, another one is to use a client handler to validate the submit button only if the listValue (or its value in the hidden widget) is right. Here is a code that does it (only the doGet is reproduced, all other functions being identical.
function doGet() {
var app = UiApp.createApplication().setTitle('test listBox');
var panel = app.createHorizontalPanel().setStyleAttribute('padding','40px');
var submitBtn = app.createSubmitButton('test').setStyleAttribute('margin-left','40').setEnabled(false);
var listHandler = app.createServerHandler('updlistVal').addCallbackElement(panel);
var items = ['category 1','category 2','category 3'];
var listVal = app.createTextBox().setText('not defined yet').setId('listboxVal').setName('listboxVal').setWidth('200');
var list1 = app.createListBox(true).setName('list1').addChangeHandler(listHandler).addItem('choose one or more item(s)');
for(var i =0;i<items.length;++i){list1.addItem(items[i])}
var clientH = app.createClientHandler().forTargets(submitBtn).setEnabled(true).validateMatches(list1, 'category');
list1.addClickHandler(clientH);
panel.add(list1).add(listVal);// set a default value in case the user is happy with that and doesn't touch the listBox
app.add(app.createFormPanel().add(panel.add(submitBtn)));
return app;
}
Using the hidden widget as validation source causes a small issue as we need to click twice on the listBox to make it work... in case there are other questions on the form this will be solved by triggering the client handler with every other widgets so that the double click won't be necessary anymore but this is becoming a bit "out of subject" I'm afraid.
EDIT 3 :
just for the fun of it, a last version that works apparently without issue...
test here
function doGet() {
var app = UiApp.createApplication().setTitle('test listBox');
var panel = app.createHorizontalPanel().setStyleAttribute('padding','40px');
var submitBtn = app.createSubmitButton('test').setStyleAttribute('margin-left','40').setEnabled(false).setId('sbmt');
var wait = app.createImage('https://dl.dropboxusercontent.com/u/211279/loading3T.gif').setId('wait').setVisible(false);
var listHandler = app.createServerHandler('updlistVal').addCallbackElement(panel);
var items = ['category 1','category 2','category 3'];
var listVal = app.createTextBox().setText('not defined yet').setId('listboxVal').setName('listboxVal').setWidth('200');
var list1 = app.createListBox(true).setName('list1').addChangeHandler(listHandler).addItem('choose one or more item(s)');
for(var i =0;i<items.length;++i){list1.addItem(items[i])}
var clientH = app.createClientHandler().forTargets(wait).setVisible(true).forTargets(submitBtn).setEnabled(false);
list1.addChangeHandler(clientH);
panel.add(list1).add(listVal);// set a default value in case the user is happy with that and doesn't touch the listBox
app.add(app.createFormPanel().add(panel.add(submitBtn).add(wait)));
return app;
}
function doPost(e){
var app = UiApp.getActiveApplication();
app.add(app.createLabel().setStyleAttribute('padding','40').setText("Submitted value(s) from list : "+e.parameter.listboxVal));
return app;
}
function updlistVal(e){
var app = UiApp.getActiveApplication();
app.getElementById('listboxVal').setValue(e.parameter.list1);
app.getElementById('sbmt').setEnabled(true);
app.getElementById('wait').setVisible(false);
return app;
}
This is a known bug in the issue tracker, Issue 959. Visit and star it for updates.
It's been known and "worked on" since Dec 2011, if you believe the notes added by the support team. Other users have provided a work-around, and a modified version of it appears below.
The idea is to attach a handler function to the ListBox, which will receive all the selected items from the ListBox. The handler will then write those values to a hidden element in the form. When the form is submitted, the list of selections will be available to the doPost(), via the hidden element.
...
var listbox = app.createListBox(true).setName("listBox").addItem("option 1").addItem("option 2");
flow.add(listbox);
// Issue 959 ListBox Workaround: http://code.google.com/p/google-apps-script-issues/issues/detail?id=959
var listboxHidden= app.createHidden("listboxHidden", "").setId("listboxHidden");
flow.add(listboxHidden);
var fixListBoxHandler = app.createServerHandler('fixListBoxHandler');
fixListBoxHandler.addCallbackElement(listbox);
listbox.addChangeHandler(fixListBoxHandler);
...
}
function fixListBoxHandler(e) {
var app = UiApp.getActiveApplication();
app.getElementById('listboxHidden').setValue(e.parameter.listbox);
return app;
}
NOTE: Unfortunately, this work-around is time-sensitive; it can take several seconds for the handler to update the hidden value. If the submit button is hit before the handler completes its job, then post() receives what was in the hidden element before the call to the handler.

Can't access to event posted values

i'm writing a little google apps script which allow to select several names among a list of developpers. But in my doPost(e) method, i can't access to the array of posted values (it's undefinded), event if i check all the checkboxes...
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// add the new menu
var menuEntries = [];
menuEntries.push({name: "Edit page", functionName: "start"})
ss.addMenu("My tools", menuEntries);
}
function start() {
var app = UiApp.createApplication();
app.setTitle("Edit page");
var formPanel = app.createFormPanel();
var mainPanel = app.createVerticalPanel();
var gridPanel = app.createGrid(1, 2);
// developpers
var developperPanel = app.createVerticalPanel()
var developpers = [];
developpers.push( "John", "Peter", "Max", "Johnny" );
for (var i = 1; i < developpers.length; ++i) {
var checkbox = app.createCheckBox(developpers[i]).setName("dev"+i);
developperPanel.add(checkbox);
}
gridPanel.setWidget(0,0,app.createLabel("Developpers : "));
gridPanel.setWidget(0,1,developperPanel);
// submit button
mainPanel.add(gridPanel);
mainPanel.add(app.createSubmitButton().setText("OK"));
formPanel.add(mainPanel);
app.add(formPanel);
var ss = SpreadsheetApp.getActive();
ss.show(app);
}
function doPost(e) {
var app = UiApp.getActiveApplication();
app.add(app.createLabel(e.values[0])); // Here is the error.
return app;
}
In the exemple, the list is fixed but in my real script, i create the list thanks to the Spreadsheet.
Thanks
Max
The correct way to see the checkboxes values is using e.parameter[name], like you said yourself on a comment. Here is some code example:
function start() {
//...
var developpers = ["John", "Peter", "Max", "Johnny"];
//you were skiping the first developer
for (var i = 0; i < developpers.length; ++i) { //array starts on zero, not one
var checkbox = app.createCheckBox(developpers[i]).setName("dev"+i);
developperPanel.add(checkbox);
}
//...
}
function doPost(e) {
var app = UiApp.getActiveApplication();
var developpers = ["John", "Peter", "Max", "Johnny"]; //repeat it here or make it global, if it's static like this
var checked = [];
for( var i in developpers )
if( e.parameter['dev'+i] == 'true' )
checked.push(developpers[i]);
app.add(app.createLabel(checked));
return app;
}
You should use e.parameter.yourCheckBoxName
for example:
function doPost(e) {
var app = UiApp.getActiveApplication();
app.add(app.createLabel(e.parameter.dev1); // Access checkbox by its name
return app;
}
This would show status of check box "Peter", when it is checked. You can modify based on your need.

An observer for page loads in a custom xul:browser

In my firefox extension I'm creating a xul:browser element. I want to have an observer that intercepts any url changes within the embedded browser and opens the url in a new browser tab (in the main browser). I'd also like new windows spawned by the xul:browser window to open in a tab instead of a new browser window.
I've created an observer which works, but I don't yet know how to apply that observer only to the xul:browser element.
function myFunction(){
var container = jQuery("#container")[0];
var new_browser_element = document.createElement('browser');
container.appendChild(new_browser_element);
var observerService = Components.classes["#mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
observerService.addObserver(myObserver, "http-on-modify-request", false);
}
var myObserver = {
observe: function(aSubject, aTopic, aData){
if (aTopic != 'http-on-modify-request'){
aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);
// alert(aSubject.URI.spec);
// Now open url in new tab
}
},
QueryInterface: function(iid){
if (!iid.equals(Components.interfaces.nsISupports) &&
!iid.equals(Components.interfaces.nsIObserver))
throw Components.results.NS_ERROR_NO_INTERFACE;
return this;
}
};
You could try:
var myObserver = {
observe: function(aSubject, aTopic, aData){
if (aTopic == 'http-on-modify-request')
{
aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);
var url = aSubject.URI.spec;
var postData ;
if (aSubject.requestMethod.toLowerCase() == "post")
{
var postText = this.readPostTextFromRequest(request);
if (postText)
{
var dataString = parseQuery(postText);
postData = postDataFromString(dataString);
}
}
var oHttp = aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);
var interfaceRequestor = oHttp.notificationCallbacks.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
var DOMWindow = interfaceRequestor.getInterface(Components.interfaces.nsIDOMWindow);
//check if it is one of your mini browser windows
if (jQuery(DOMWindow).hasClass('mini_browser'))
{
openInTab(url, postData);
var request = aSubject.QueryInterface(Components.interfaces.nsIRequest);
request.cancel(Components.results.NS_BINDING_ABORTED);
}
}
},
QueryInterface: function(iid){
if (!iid.equals(Components.interfaces.nsISupports) &&
!iid.equals(Components.interfaces.nsIObserver))
throw Components.results.NS_ERROR_NO_INTERFACE;
return this;
},
readPostTextFromRequest : function(request) {
var is = request.QueryInterface(Components.interfaces.nsIUploadChannel).uploadStream;
if (is)
{
var ss = is.QueryInterface(Components.interfaces.nsISeekableStream);
var prevOffset;
if (ss)
{
prevOffset = ss.tell();
ss.seek(Components.interfaces.nsISeekableStream.NS_SEEK_SET, 0);
}
// Read data from the stream..
var charset = "UTF-8";
var text = this.readFromStream(is, charset, true);
// Seek locks the file so, seek to the beginning only if necko hasn't read it yet,
// since necko doesn't seek to 0 before reading (at lest not till 459384 is fixed).
if (ss && prevOffset == 0)
ss.seek(Components.interfaces.nsISeekableStream.NS_SEEK_SET, 0);
return text;
}
else {
dump("Failed to Query Interface for upload stream.\n");
}
}
return null;
},
readFromStream : function(stream, charset, noClose)
{
var sis = Components.classes["#mozilla.org/binaryinputstream;1"]
.getService(Components.interfaces.nsIBinaryInputStream);
sis.setInputStream(stream);
var segments = [];
for (var count = stream.available(); count; count = stream.available())
segments.push(sis.readBytes(count));
if (!noClose)
sis.close();
var text = segments.join("");
return text;
}
};
function openInTab(url, postData)
{
var wm = Components.classes["#mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator);
var recentWindow = wm.getMostRecentWindow("navigator:browser");
if (recentWindow)
{
// Use an existing browser window, open tab and "select" it
recentWindow.gBrowser.selectedTab = recentWindow.gBrowser.addTab(url, null, null, postData);
}
}
function parseQuery() {
var qry = this;
var rex = /[?&]?([^=]+)(?:=([^&#]*))?/g;
var qmatch, key;
var paramValues = {};
// parse querystring storing key/values in the ParamValues associative array
while (qmatch = rex.exec(qry)) {
key = decodeURIComponent(qmatch[1]);// get decoded key
val = decodeURIComponent(qmatch[2]);// get decoded value
paramValues[key] = val;
}
return paramValues;
}
function postDataFromString(dataString)
{
// POST method requests must wrap the encoded text in a MIME
// stream
var stringStream = Components.classes["#mozilla.org/io/string-input-stream;1"]
.createInstance(Components.interfaces.nsIStringInputStream);
if ("data" in stringStream) // Gecko 1.9 or newer
stringStream.data = dataString;
else // 1.8 or older
stringStream.setData(dataString, dataString.length);
var postData = Components.classes["#mozilla.org/network/mime-input-stream;1"].
createInstance(Components.interfaces.nsIMIMEInputStream);
postData.addHeader("Content-Type", "application/x-www-form-urlencoded");
postData.addContentLength = true;
postData.setData(stringStream);
return postData;
}
I'll update this to fill in the blanks in a bit.
edit: see http://forums.mozillazine.org/viewtopic.php?p=2772951#p2772951 for how to get the source window of a request.
Request cancellation code from http://zenit.senecac.on.ca/wiki/index.php/Support_For_OpenID.
see http://mxr.mozilla.org/mozilla-central/source/netwerk/base/public/nsIRequest.idl for details on nsIRequest.
See http://forums.mozillazine.org/viewtopic.php?p=2404533#p2404533 and https://developer.mozilla.org/en/XUL/Method/addTab for the definition of addTab.
parseQuery comes from http://blog.strictly-software.com/2008/10/using-javascript-to-parse-querystring.html.
See https://developer.mozilla.org/en/Code_snippets/Post_data_to_window#Preprocessing_POST_data for how to process post data in a form suitable for addTab.
ReadPostFromText and ReadTextFromStream both come from firebug (though slightly modified)

Resources