1

I have been learning javascript, jQuery, and userscripts, as well as how to user firebug, the last 3 days in the attempt to display information about a selected customer on the website desk.com. So far, other than a lot of learning, I have little to show for. I simply can't get the message to update when a new tab is selected.

The purple tab is the selected one. All I need to do is when a new tab is selected display a message. For testing lets just put this message in an javascript alert(). Here is the HTML for the entire top bar that contains the Customer 1 tab.

<ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner all">
  <li class="a-icons ui-state-default ui-corner-top">
    <a id="tab-tickets-label" href="#tabs-tickets">
      <img width="18" height="16" src="https://d3jyn100am7dxp.cloudfront.net/images/a-icon-agent-list.png?1352530323" alt="">
     </a>
  </li>
  <li class="a-icons ui-state-default ui-corner-top">
    <a id="tab-kb" href="#tabs-kb">
      <span>
        <img width="16" height="17" src="https://d3jyn100am7dxp.cloudfront.net/images/a-icon-knowledge-base.png?1352530323" alt="">
      </span>
    </a>
  </li>
  <li class="a-icons a-last ui-state-default ui-corner-top">
    <a id="tab-search" href="#tabs-search">
      <span>
        <img width="18" height="17" src="https://d3jyn100am7dxp.cloudfront.net/images/a-icon-search.png?1352530323" alt="">
      </span>
    </a>
  </li>
  <li class="ui-state-default ui-corner-top">
    <a id="tab_header_ticket_id_73354627" href="#tabs-tid_88be8cc0-116b-0130-bf8a-12313f070061">
      <span id="tabs-tid_88be8cc0-116b-0130-bf8a-12313f070061-closer" class="tab_closer">
      </span>
      <span>
      <span id="tabs-tid_88be8cc0-116b-0130-bf8a-12313f070061-icon" class="top_tab_icon">
        <img width="20" height="20" src="https://d3jyn100am7dxp.cloudfront.net/images/interactions/20/a-phone.png?1352530323" alt="">
      </span>
      <span id="tabs-tid_88be8cc0-116b-0130-bf8a-12313f070061-customer-name" class="top_tab_customer_name">Customer 2</span>
      </span>
      <span id="tabs-tid_88be8cc0-116b-0130-bf8a-12313f070061-service-level-bar" class="tab_service_level_indicator"> </span>
    </a>
  </li>
  <li class="ui-state-default ui-corner-top ui-tabs-selected ui-state-active">
    <a id="tab_header_ticket_id_73382205" href="#tabs-tid_88cd2df0-116b-0130-1271-12313f06c46f">
      <span id="tabs-tid_88cd2df0-116b-0130-1271-12313f06c46f-closer" class="tab_closer"></span>
      <span>
        <span id="tabs-tid_88cd2df0-116b-0130-1271-12313f06c46f-icon" class="top_tab_icon">
          <img width="20" height="20" src="https://d3jyn100am7dxp.cloudfront.net/images/interactions/20/a-email.png?1352530323" alt="">
        </span>
        <span id="tabs-tid_88cd2df0-116b-0130-1271-12313f06c46f-customer-name" class="top_tab_customer_name">Customer 1</span>
      </span>
      <span id="tabs-tid_88cd2df0-116b-0130-1271-12313f06c46f-service-level-bar" class="tab_service_level_indicator"> </span>
    </a>
  </li>
</ul>

In the above code the element with the class 'ui-tabs-selected' is a <li>. This class is added to the newly selected tab every time a new tab is selected. I need my script to display a message, say "a new customer is selected", every time the class 'ui-tabs-selected' is added to a new element.

Here is the scriptAttemp.user.js that I have tried most recently.

// ==UserScript==
// @name          deskNotifications2
// @namespace     http://www.dutgriff.com/userscripts
// @description   Notifications for desk.com
// @include       https://nvows.desk.com/agent*
// @version       1.0
// @require       http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require       https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant         GM_addStyle
// ==/UserScript==

waitForKeyElements (
    ".ui-tabs-selected", addAlert
);

function addAlert (jNode) {
    alert('The user was selected.');
}


The above script works great at first. It will display the message when the page loads. It will display the message again if I select a new tab. Then when I select a new tab again it does nothing.

I need it to display the message every time a new tab is selected, or every time the '.ui-tabs-selected' is moved to a new location. This class can move to a new location by clicking other things on the page, not just by clicking on a different tab.

For instance when there are no customer-tab selected I can click a button to view a new customer. This will open a new tab and place the '.ui-tabs-selected' on this new tab. All advice/links to info pages/constructive criticism is greatly appreciated. Sorry I am so new to all of this.

4

1 回答 1

1

By default, waitForKeyElements() only fires once per individual node found. But, what you want is to fire when that class (ui-tabs-selected) moves from one node to another.

This means that you must track the old node, and see if it is the same as the new node. And if you use waitForKeyElements(), you must tell it to run more than once on individual found nodes. You can do that by having the callback function (addAlert() in this case) return true.

Putting it all together, your script would become:

// ==UserScript==
// @name          deskNotifications2
// @namespace     http://www.dutgriff.com/userscripts
// @description   Notifications for desk.com
// @include       https://nvows.desk.com/agent*
// @version       1.0
// @require       http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require       https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant         GM_addStyle
// ==/UserScript==

waitForKeyElements (
    ".ui-tabs-selected", addAlert
);

function addAlert (jNode) {
    if (typeof this.lastNode == "undefined"  ||  this.lastNode != jNode[0]) {
        alert('The user was selected.');
    }
    this.lastNode = jNode[0];

    return true;    // Allow repeat firings for this node.
}
于 2012-11-16T03:15:35.210 回答