mercredi 16 novembre 2016

How to write a userscript that pulls an email address out of Zendesk?

I've written a Tamper Monkey script (Chrome) to pull an email address out of our Zendesk ticket, and then appended a link to lookup that user in our admin tools in another tab. It works, but only consistently if you refresh the page when you arrive at a new ticket (otherwise, you end up with the value of the last ticket you looked up).

Based on inspecting the elements, it looks like Zendesk uses Ember. This means that Zendesk has its own tabs internal to the page, so that when you click around in the site, you don't always necessarily re-load the page.

I can't figure out how to get it to always be up to date (with value of email address of current ticket) without a refresh, and I also couldn't figure out how to get it to work without the timeOut... When I did it based on document ready or onload window, it doesn't work right. I'm suspecting my poor understanding of Embed load states to be to blame here as well.

PS. I'm using noConflict because I think Ember also uses the $ sign, but also not sure if it's necessary. Any help would be appreciated! THANKS!

// ==UserScript==
// @name         User lookup
// @namespace    Metronome
// @version      0.2
// @description  Checks ticket email address and looks up in admin tools
// @author       Tphlat
// @include      http://ift.tt/2fYAhs1
// @grant        none
// ==/UserScript==

$.noConflict();

    jQuery( document ).ready(function() {
        console.log( "DOCUMENT LOADED" );
    });

    jQuery(window).on('load', function () {
        console.log( "WINDOW LOADED" );
     //   alert('READY WINDOW');
    });

window.onload = function() {
    function myFunc(){
        var email = document.querySelector("a.email").innerHTML;
        email = email.replace(/\@/g,'%40');
console.log(email);
        var linkOut = " <strong><a href='http://ift.tt/2fGGvK7" + email + "' target='_blank' style='background-color:yellow;'>\/  LOOKUP  \/</a></strong>";
        jQuery('div.source.delimited_items').append(linkOut);
    }
     setTimeout(myFunc, 2000);
};



Aucun commentaire:

Enregistrer un commentaire