From time to time, Salsa Classic clients want to change an element on a page. That's a snap when the element already appears on the page. However, fields that appear in targeted advocacy actions may not appear on the first displayed page.
Targeted advocacy actions in Salsa Classic are executed as a series of pages. The pages are loaded into the same HTML page using AJAX. The general flow of pages is
- Gather address and zip.
- Provide personal information and custimize the letter to the target(s).
- Submit the letter.
- (Optional) provide extra information that the targets need.
Note that each of the pages is loaded into the same location in the targeted action. Pages appear exclusively and can't be modified after they've been replaced.
The challenge is, in a lot of cases, that Salsa Classic clients would like to modify the content of the Letter page. These changes, are typically
- Hide the target's contact information (phone and/or email),
- Change the appearance of one or more fields on the Letter page, or
- Add a messages or some text around input fields.
A standard script won't work for these purposes. Scripts run when the page is loaded into the browser. The Letter page hasn't arrived when the page is showing. A script in the page HTML will run but won't actually change anything.
The solution is to have a script wait for a field to appear. For example, wait for the Legislator contact information so that it can be hidden. We've cooked up an way to wait for a field without using up a lot of browser resources.
TL;DR The solution looks for the field to be modified. If it's already part of the page, there the field is modified immediately and the job is done. If the field does not appear, then the solution uses a MutationObserver to wait for the field of your chose to appear.
The workhorse of this solution is the waitForElement()
method on this page.
- Determine the selector for the root element that needs to be watched. For example, the selector for the root element in targeted actions is
#mainForm
. - Determine the selector for the element that you're waiting for. Some examples:
- Cell_Phone:
"input[name=Cell_Phone]"
- Contact information:
"div.recipient .number"
- State:
"select[name=State]"
- Cell_Phone:
- Write a function named
modify()
that will modify the element once it's found. Themodify()
function will be called for each of the elements that match the provided element selector.
You only need to install the waitForElement.html
file one time, even if you are using waitForElement()
to make several modifications on more than one page. Insert the modify()
function as shown in waitForElement.html
.
If you do have more than one modify()
on a page, then
- Rename the
modify()
functions to reflect what is being modified. For example,modifyPhoneNumber()
. - Add a
waitForElement()
call that is configured for your modify function. See the examples (below). - Test, test, test!
- Edit a template used by actions.
- Use the browser's search tool to find the
</body>
tag. - Insert a blank line before the
</body>
tag.
- Click here to go to the waiter source.
- Locate and click the "Raw" button. A new window will appear that contains just the solution.
- Copy the contents of the window and close it.
- Return to the template editor.
- Paste the copied text into the newly inserted blank line.
- Click on one of these links to view the modify function.
- Locate and click the "Raw" button . A new window will appear that contains just the solution.
- Copy the contents of the window and close it.
- Return to the template editor.
- Locate the marker that shows where to put the modify function.
/* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
PUT YOUR modify() FUNCTION HERE.
PUT YOUR waitForElement() CALL HERE.
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ */
- Replace the marker with the text copied from the README file.
- Save the template.
- Test!
The script to change the content of the "District:" line contains a special version of the wait handler. If you'd like to this script in your website, then
- Download the script from this page to your disk.
- Upload the script to your Salsa Classic instance. Click here if you need help with that.
- Uploading the script will result in a URL for the uploaded file. Copy that.
- Choose the template used by your Advocacy actions.
- Insert this text just before the tag. Replace
[[the script's URL]]
with the URL copied in step 3.
<!-- BEGIN Change "District" line on the contact page for actions. -->
<script type="text/javascript" src="[[the script's URL]]"></script>
<!-- END Change "District" line on the contact page for actions. -->
If you have any questions, then please collect this information and send it to support@salsalabs.com. We'll be glad to help.
- Your
organization KEY
- Your
chapter KEY
if you are in a chapter of another organization - The
template_KEY
of the modified - The URL of an action that uses the template.
- A screenshot of the action showing the issue (if possible)
Send this information to support@salsalabs.com. We'll be glad to help.