Greasy Fork

Shined up real nice.

TradeMe Address Check

Add Link To Check An Address

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// ==UserScript==
// @name        TradeMe Address Check
// @namespace   https://greasyfork.org/scripts/3143-address-check
// @version     0.34
// @description Add Link To Check An Address
// @include     https://www.trademe.co.nz/MyTradeMe/Delivery/SendDeliveryAddress.aspx*
// @include     https://www.trademe.co.nz/MyTradeMe/Sell/Sold.aspx*
// @copyright   public domain
// @author      Godfrey Livingstone godfrey@satelliteshop.co.nz
// @run-at	document-end
// @require	https://greasyfork.org/scripts/2722-gm-config-mod-library/code/gm_config_mod%20library.js?version=7536
// @grant	GM_addStyle
// @grant	GM_getValue
// @grant	GM_log
// @grant	GM_registerMenuCommand
// @grant	GM_setValue
// @grant	GM_xmlhttpRequest
// @grant	GM_deleteValue
// ==/UserScript==
// v0.31:	Remove all jquery so that it works with GM 2.0 aand TM 3.8
// v0.32:	Fix for TradeMe Changes
// v0.33:	Add UserName to URL
// v0.34:	Fix for Chrome UserName added to URL does not work for chrome


if (window.top != window.self) { //-- Don't run on frames or iframes
    return;
}

// replace trademe's JS error handler
window.onerror=function(msg, url, linenumber){
    console.log('Error message: '+msg+'\nURL: '+url+'\nLine Number: '+linenumber);
    return true;
};

//
// Delivery Address
// http://www.trademe.co.nz/MyTradeMe/Delivery/SendDeliveryAddress.aspx?asid=<purchase id>
//
// Delivery Address
// Phone Number
// Message

//
// Purchase Summary
// http://www.trademe.co.nz/MyTradeMe/PurchaseSummary.aspx?asid=<purchase id>&isBuyer=false
//
// Shipping Option

//
// Listing
// http://www.trademe.co.nz/Browse/Listing.aspx?id=<Listing ID>
//
// Title
// Photo Link

//
// Photo
// http://images.trademe.co.nz/photoserver/tq/263983945.jpg
//
// encodeURIComponent() will not encode: ~!*()'
// separate function to add '!' to encodeURIComponent
// removes hard returns
// lastly replaces spaces with '+'
function encodeURIfix(str) {
    return encodeURIComponent(str).replace(/!/g,'%21')
                                  .replace(/%0A/g,'%20')
                                  .replace(/%20/g,'+');
}

// Prototype for the callback function - remember which purchase
Function.prototype.bind = function (thisObject) {
    var method, oldargs, newargs;
    method = this;
    oldargs = [].slice.call(arguments, 1);
    return function () {
        newargs = [].slice.call(arguments);
        return method.apply(thisObject, oldargs.concat(newargs));
    };
};

function checkAddressLink(doc, delivery_address){
    var add_html, add_array, add_str, encoded_add, namestr, encoded_name, ph, encoded_ph, message, encoded_message, encoded_addassent, link_url;

    add_html = delivery_address.innerHTML;
    add_array = add_html.split("<br>");
    add_str = add_array.slice(1, -1).join(', ');
    encoded_add = encodeURIfix(add_str);
    namestr = add_array[0];
    encoded_name = encodeURIfix(namestr);

    ph = doc.getElementById("PhoneNumber");
    encoded_ph = "";
    if (ph) {
        encoded_ph = encodeURIfix(ph.innerHTML);
    }

    message = doc.getElementById("Message");
    encoded_message = "";
    if (message){
        encoded_message = encodeURIfix(message.innerHTML);
    }

    encoded_addassent = encodeURIfix(add_html);

    link_url = "http://" + userName + ".addresscheck.co.nz/?address=";
    link_url = link_url + encoded_add + '&name=' + encoded_name + '&phone=' + encoded_ph + '&address_as_sent=' + encoded_addassent + '&message=' + encoded_message;

    return link_url;
}

function addSoldDeliveryCheck(dL) {
    var doc, delivery_address, link_url, add_image, add_link, newTr, td_start, td_end;
    //var purchase_summary_url = 'http://www.trademe.co.nz/MyTradeMe/PurchaseSummary.aspx?asid=' + pReference + '&isBuyer=false';

    //turn the response text into a dom object
    doc = document.implementation.createHTMLDocument("delivery address");
    doc.documentElement.innerHTML = dL.target.responseText;

    delivery_address = doc.getElementById("DeliveryAddress");

    if ( delivery_address ) {

        link_url = checkAddressLink(doc, delivery_address);

        add_image = '<a id="AddressImageCheckLink" target="_blank" href="' + link_url + '"><img  width="18" height="17" alt="" style="vertical-align: middle; margin-right:5px; border:0;"src="/images/1pixel.gif"></a>';
        add_link =  '<a id="AddressCheckLink" target="_blank" href="' + link_url + '"> * Check address * </a>';

        newTr = document.createElement("tr");

        td_start = '<td style="vertical-align: middle;"></td><td style="color: #666;" align="left"><small>';
        // td_start = '<td style="vertical-align: middle;"><img alt="" src="/images/my_trademe/tick2.gif"></td><td style="color: #666;" align="left"><small>';
        td_end = '</small></td>';

        newTr.innerHTML = td_start + add_link + td_end;
        this.deliveryDetails.parentNode.parentNode.parentNode.parentNode.appendChild(newTr);

    }
    doc.parentNode.removeChild(doc);
}

// Get userName
var USERNAME_STORAGE_ITEM = "trademeusername";

var userName = sessionStorage.getItem(USERNAME_STORAGE_ITEM);
if (!userName || userName==="") {
    GM_xmlhttpRequest({
        method: "GET",
        url: "https://www.trademe.co.nz/MyTradeMe/Default.aspx",
        onload: function(data) {
            userName = $('#MemberLink', data.responseText).text();
            sessionStorage.setItem(USERNAME_STORAGE_ITEM, userName);
            }
    });
}

if (!userName || userName==="") {
    userName = "www"
    sessionStorage.setItem(USERNAME_STORAGE_ITEM, userName);
}

// Are we on the SendDeliveryAddress page?
var delivery_address, link_url, add_image, add_link, newDiv;

delivery_address = document.getElementById("DeliveryAddress");
if ( delivery_address ) {
    link_url = checkAddressLink(document, delivery_address);
    add_image = '<a id="AddressImageCheckLink" target="_blank" href="' + link_url + '"><img  width="18" height="17" alt="" style="vertical-align: middle; margin-right:5px; border:0;"src="/images/1pixel.gif"></a>';
    add_link =  '<a id="AddressCheckLink" target="_blank" href="' + link_url + '">Check address</a>';
    newDiv = document.createElement("div");
    newDiv.setAttribute("style", "font-size: 12px; margin-top: 10px; padding: 0;");

    newDiv.innerHTML = add_image + add_link;
    delivery_address.parentNode.appendChild(newDiv);
}

// Are we on the Sold Page and have any 'Delivery address' details been provided?
var frm, i, j, l, m, els, els2, el, el2, pReference, oReq, auction_array;

frm = document.getElementById("form1");
if (frm) {
    els = frm.getElementsByTagName("a");
    for ( i = 0, l = els.length; i < l; i++) {
        el = els[i];
        if (el.innerHTML === 'Delivery address') {
           pReference = el.href.replace(/.*asid=/i, '');
           oReq = new XMLHttpRequest();
           oReq.open("GET", el.href);
           // register the event handler
           oReq.addEventListener("load", addSoldDeliveryCheck.bind({
                deliveryDetails: el
            }), true);
          oReq.send();
        }
    }
    els2 = frm.getElementsByTagName("dt");
    for ( j = 0, m = els2.length; j < m; j++) {
        el2 = els2[j];
        if (el2.innerHTML === "Listing #:") {
            auction_array = el2.nextSibling.innerHTML.split(";");
            console.log(auction_array[0]);
            console.log(auction_array[1]);
            el2.nextSibling.innerHTML = auction_array[0] + ';<a target="_blank" href="http://www.trademe.co.nz/Browse/Listing.aspx?id=' + auction_array[1] + '">' + auction_array[1] + '</a>';
        }
    }
}