freekode / Improve Gumtree

// ==UserScript==
// @name         Improve Gumtree
// @namespace    http://freekode.org/
// @version      0.1
// @description  add two buttons, open in new tab, and hide an ad
// @author       You
// @match        http://*.gumtree.pl/*
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==


//GM_setValue('', 'yes')
ads = GM_getValue('ads', {"excluded": []})
console.log('hidden ads = ' + ads)

OpenAdButton = function(link) {
    this.link = link;
    this.getButton = function() {
        return $('<a target="_blank" href="' + this.link + '">' +
                 	'<button class="newButton">Open</button>' +
                 '</a>')
    }
}

HideButton = function(id) {
    this.id = id;
    this.getButton = function() {
        return $('<button class="searchButton" onClick="hideAd(\'' + this.id + '\', this)">Hide</button>')
    }
}


hideAd = function(id, elem) {
    $(elem).closest('tr.resultsTableSB').remove()
    ads.excluded.push(id)
    GM_setValue('ads', ads)
}

clearHidden = function() {
    ads.excluded = []
    GM_setValue('ads', ads)
    location.reload()
}



$('.sbInnerDiv').prepend(
    $('<table><tr>' +
          '<td><button class="searchButton" onClick="clearHidden()">Clear hidden</button></td>' +
          '<td>Hidden = ' + ads.excluded.length + '</td>' +
      '</tr></table>'))


$('#SNB_Results > tbody > tr.resultsTableSB').each(function(i, elem) {
    row = $(elem)
    link = row.find('a.adLinkSB').first().attr('href')
    if (link == undefined) {
		return;
    }
    
    id = link.match(/.*-(\d*)/)[1]

    state = false
    ads.excluded.map(function(elem) {
        if (elem == id) {
            state = true
        }
    })
    if (state) {
        row.remove()
        return;
    }
    
    $(elem).append(new OpenAdButton(link).getButton())
    $(elem).append($('<br/>'))
    $(elem).append(new HideButton(id).getButton())
})