Help clicking a button in Greasemonkey - button

1st- I'm not a programmer, so assume I know nothing about JavaScript. In fact, I just figured out that Greasemonkey is JavaScript.
2nd - But I have been searching and reading for several days trying to do it myself. I'm not lazy, it's just that I'm in way over my head.
http://userscripts.org/scripts/review/57265
The program automatically clicks a radio button and adds a new button. When you click the new button it automatically fills text in the box. I'm trying to simply modify this Greasemonkey Script to automaticaly click the new button.
But I've learned this isn't so simple. Apparently I need an XPath for this? That's about all I've figured out so far. I really don't want to learn all of JavaScript just to do this one thing.
I've tried inserting this in at the end:
function click(elm) {
var evt = document.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, window, 0, 1, 1, 1, 1, false, false, false, false, 0, null);
elm.dispatchEvent(evt);
It didn't work. Maybe I'm missing something?
I also tried this from another program:
var rep = document.getElementById("report");
if( (rep != null) ) ) {
var repk = rep.childNodes;
for( var repidx=0; repidx<repk.length; repidx++ ) {
var rep2 = repkrepaidx];
}
But again I don't think I put it in right?

assuming the "report" in your example is a form, you can just submit it.
form.submit
If the element used to submit the form is input type="submit" or input type="button", you can "click" it.
buttonElement.click();

Related

Button change text display every after click

I am new in Google script. Could you please help me on this.
I want to create a button in googlesheet with series of text display every after click.
( I dont know what to put in script)
OPEN (green background) --> CLICK--> CLOSE ( red background) --> CLICK--> OPEN (green background)...and so on
Thank you in advance
I believe your goal as follows.
You want to achieve the button which is changed when the button is clicked on the Google Spreadsheet.
For example, when the button with the green color and "OPEN" text is clicked, you want to change it to the button with the red color and "CLOSE" text.
You want to achieve this using Google Apps Script.
Issue and workarounds:
In the current stage, unfortunately, Google Apps Script cannot directly manage the drawing on Google Spreadsheet by editing the text and color. So it is required to think of the workaround for achieving your goal. In this answer, I would like to propose the following 3 workarounds as the methodology.
Workaround 1:
In this workaround, the drawing is used as the button. When the drawing is used as the button, 2 drawings are prepared and both drawings are overwrapped. Each button has the function of button1 and button2, respectively. When a button is clicked, the z index is replaced. By this, the switching button can be achieved. Because in the current stage, the image cannot be directly edited by the Google Apps Script.
Usage:
In order to use the sample script, create 2 buttons as the drawings like the above demo image.
In this case, please overwrap the both drawings.
Please assign the function of button1 and button2 to each button.
Copy and paste the following script to the container-bound script of Spreadsheet and save it.
const button1 = () => switching("button1");
const button2 = () => switching("button2");
function switching(name) {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
const drawings = sheet.getDrawings();
drawings.forEach(d => d.setZIndex(d.getOnAction() == name ? 0 : 1));
const temp = ss.insertSheet();
SpreadsheetApp.flush();
ss.deleteSheet(temp);
sheet.activate();
}
Click the button. By this, the script is run and the button is switched.
Note:
In this case, in order to refresh the change of z index of drawings, it is required to change the focus to other sheet. By this, when the button is clicked, the button is flashing in a moment. About this, I would like to expect the future update.
Workaround 2:
In this pattern, the drawing is used as the button. In this case, 2 drawings are also prepared, and one button is put to the cell "C3" and another one is other position (as the test, the button is put to "E10".) in the same sheet. Each button has the function of button1 and button2, respectively. When a button is clicked, the clicked button is replaced with the button of the another position. By this, the switching button can be achieved.
Usage:
In order to use the sample script, create 2 buttons as the drawings like the above demo image.
In this case, please put a button to the cell "C3" and another one to other position (as the test, the button is put to "E10".) in the same sheet.
Please assign the function of button1 and button2 to each button.
Copy and paste the following script to the container-bound script of Spreadsheet and save it.
const button1 = () => switching("button1");
const button2 = () => switching("button2");
function switching(name) {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
const drawings = sheet.getDrawings();
if (drawings[0].getOnAction() == name) {
drawings[1].setPosition(3, 3, 0, 0);
drawings[0].setPosition(10, 5, 0, 0);
} else {
drawings[0].setPosition(3, 3, 0, 0);
drawings[1].setPosition(10, 5, 0, 0);
}
}
Click the button. By this, the script is run and the button is switched.
Note
In this case, it is not required to refresh by focusing to other sheet. But, another button is required to be put to the same sheet.
Workaround 3:
In this workaround, the cell is used as the button. When the cell is used as the button, in this case, the cell "C3" is used. And the OnSelectionChange event trigger is used for executing the script.
Usage:
In order to use the sample script, put the text of "OPEN" and the red color to the cell "C3" like above demo image.
Copy and paste the following script to the container-bound script of Spreadsheet and save it.
function onSelectionChange(e) {
const range = e.range;
const sheet = range.getSheet();
const a1Notation = range.getA1Notation();
if (a1Notation == "C3") {
if (range.getValue() == "OPEN") {
range.setBackground("#ff0000");
range.setValue("CLOSE");
} else {
range.setBackground("#38761d");
range.setValue("OPEN");
}
sheet.getRange("A1").activate();
}
}
Click the cell "C3". By this, the script is run and the text and color of the cell are switched.
Note
In this case, in order to select the button again, it is required to change the selected cell. This is the current specification.
Note:
These are the simple sample scripts. So please modify them for your actual situation.
References:
Class Drawing
Simple Triggers

SalesForce: Can you open a text field with a custom button

I am building a custom button and I have this so far:
{!REQUIRESCRIPT("/soap/ajax/8.0/connection.js")}
var lead = new sforce.SObject("Lead");
lead.id = "{!Lead.Id}";
lead.OwnerId = "ID";
lead.Status = "STATUS";
var result = sforce.connection.update([lead]);
if (result[0].getBoolean("success"))
{
// Refresh window
window.location.reload();
}
else
{
alert("Error saving lead");
}
This all works perfectly and makes the changes I desire, but what I also want to happen is when the button is clicked, and before anything is saved, I want the:
lead.Reason__c
Text field to pop up, like it would if you double clicked it, so that the user can fill in a reason for clicking the button
I have tried code like:
lead.Reason__c.open
lead.Reason__c.edit
But have had no luck and am pretty much just stabbing in the dark with this.
I hope someone can help,
Thanks all
Since you only have one field, you do not need a form, I would recommend you use javascript's prompt box and if it returns something other than null (null -> user clicked cancel) do your magic
{!REQUIRESCRIPT("/soap/ajax/8.0/connection.js")}
var reason = prompt("Enter reason", "");
if (reason != null) {
var lead = new sforce.SObject("Lead");
lead.id = "{!Lead.Id}";
lead.OwnerId = "ID";
lead.Status = "STATUS";
lead.Reason__c = reason;
...
}
PS: I would also recommend using a "fresher" ajax toolkit than 8.0 :) we are at 24.0 now. I doubt they'll obsolete the old ones but you never know.
The correct way to truly simulate a double-click is by dispatching a double-click event to the element; this page has an excellent reference on how to do that. I can confirm that you can create a custom button that dispatches a double-click to an SFDC edit field and it acts as if the user double-clicked the field themselves. Quick and dirty example:
var evObj = document.createEvent('MouseEvents');
evObj.initMouseEvent( 'dblclick', true, true, window, 1, 12, 345, 7, 220, false, false, true, false, 0, null );
document.getElementById('lea13_ilecell').dispatchEvent(evObj);
However, that's not going to be a sound approach for you because once you double-click a field, as you know, the "Save" and "Cancel" buttons are rendered to the user with no ability to override their behavior in the manner you need to. You could hack around this with some jQuery magic, but I'd recomment mmix's approach, it's much more sound. If your requirements expand to something beyond a single text field, you may need to incorporate some fancy dynamic HTML/CSS dialog boxes, or VisualForce, but based on what you need, the prompt approach is the simplest answer.

JQuery updating label

I have 2 textboxes and a label on my page. The 2 textboxes will contain numeric values. The label text will be the product of the 2 textbox values. Is there a way to do this using JQuery so that the value can get updated when I edit the textboxes without having to do a postback?
Also the textboxes may contain values with commas in it: e.g. 10,000. Is there a way I can extract the number from this so that it can be used to calculate the label value.
Thanks in advance,
Zaps
I can't add comments to other answers yet, so I'll just post an update here.
The original question involved product, which means multiplication, so here's a version that allows for unlimited textboxes and completes the multiplication.
function makeInt(text) {
return parseInt(text.replace(',', ''));
}
$(function(){
//hook all textboxes (could also filter by css class, if desired)
//this function will be called whenever one of the textboxes changes
//you could change this to listen for a button click, etc.
$("input[type=text]").change(function(){
var product = 1;
//loop across all the textboxes, multiplying along the way
$("input[type=text]").each(function() {
product *= makeInt($(this).val());
});
$("#display-control-id").html(product);
});
});
$('#SecondTextbox').keyup(function() {
var t1 = $('#FirstTexbox').val();
var t2 = $(this).val();
var result = t1+t2;
$('#resultLabel').html(result);
});
This could do the trick, or you could have it on a click event with some link element. This would also not have any page refresh.
$('#checkButton').click(function() {
var t1 = $('#FirstTexbox').val();
var t2 = $('#SecondTextbox').val();
var result = t1+t2;
$('#resultLabel').html(result);
});
Link could be something like,
<a id="checkButton" title="Check your result">Check</a>
And with that you would have the css settings of 'cursor:pointer;' to make it seem a proper link.

jQuery Autocomplete losing text on AutoPostBack

I have a jQuery Autocomplete field on an ASP.Net Webform and everything has been working great until now. I also have a DropDownList that I have a need to fire onSelectedIndexChanged with AutoPostBack.
When I changed my code to do the AutoPostBack, the text field that has the jQuery AutoComplete on it comes back blank. However, if I look at the source of the page, the text is in the text field. If I now post the form, the page will send back a blank field. My Google-Fu is weak on this one, as I could not come up with any workaround for it.
Has anyone had any issues like this with the Autocomplete field getting blanked out on an AutoPostBack, and how did you get around it?
I can post code if it's really necessary, but I'd need to sanitize a lot of it before I could due to company policy.
How is the autocomplete field being initialized? Is it being set to empty string either on page load from server or by the autocomplete jQuery plugin on document.ready?
If the source code for the plug-in is setting the textbox to empty string on document.ready then try the following:
// Store current textbox value in a var
var temp = $('#mytextbox').val();
// Initialize the autocomplete plugin (winging it on the syntax)
$('#mytextbox').autocomplete();
// Reset the value of the textbox.
$('#mytextbox').val(temp);
If you use jQuery Autocomplete plugin 1.1,
* Revision: $Id: jquery.autocomplete.js 15 2009-08-22 10:30:27Z joern.zaefferer $
Add "autoPostBackSelection: false," in the options Ex:
$.Autocompleter.defaults = {
inputClass: "ac_input",
resultsClass: "ac_results",
loadingClass: "ac_loading",
minChars: 1,
delay: 400,
autoPostBackSelection: false,
...
After, add this just before the "return true; at the end of the "selectCurrent() function.
if (options.autoPostBackSelection == true) {
__doPostBack($input.id, "");
}
Example:
function selectCurrent() {
...
if (options.autoPostBackSelection ==
true) {
__doPostBack($input.id, "");
}
return true;
}

Events Overwritten in ASP.AJAX on IE7

Greetings!
I'm calling a Web service from Javascript when a user clicks on a link. I need to get the coordinates where the user clicked so that I can display a DIV in an appropriate location. My client-side script looks like the following:
var g_event;
function DoWork(event, theId)
{
if (IsIE())
g_event = window.event;
else
g_event = event;
Acme.WebServices.Worker.GetInformation(theId, DoWorkSuccess);
}
function DoWorkSuccess(result)
{
var l_elemDiv = document.getElementById("content-area-div");
DisplayAreaDiv(g_event, l_elemDiv, result);
}
It's used like this:
Help
This works great in Firefox, Safari, and Opera. In IE7, not so much. For example, if I place the following code at the end of both the DoWork() and DoWorkSuccess() functions:
alert(g_event.clientX + ", " + g_event.clientY);
In IE, I'll get two alerts; the first one has correct coordinates, but the second one (which displays on top of the first one) is simply "[object]". Since that "[object]" one is the last one, my DIV is incorrectly displayed in the top left of the browser window. Is there a way I can prevent IE from giving me a second "bad" event? Thanks.
Why not extract and save the coordinates in DoWork and simply use them in DoWorkSuccess rather than saving the event. Of course this won't work if there is more data you are extracting from the event.
var client_x;
var client_y;
function DoWork(event, theId)
{
var g_event;
if (IsIE())
g_event = window.event;
else
g_event = event;
client_x = g_event.clientX;
client_y = g_event.clientY;
Acme.WebServices.Worker.GetInformation(theId, DoWorkSuccess);
}
function DoWorkSuccess(result)
{
var l_elemDiv = document.getElementById("content-area-div");
DisplayAreaDiv( { clientX : client_x, clientY : client_y }, l_elemDiv, result);
}
Have you tried setting window.event.cancelBubble = true in your DoWork function?
If not, quirks mode has good article on events and event bubbling - http://www.quirksmode.org/js/events_order.html that has helped me a lot with these kinds of issues.

Resources