Advertisement
Guest User

GumtreeFilter

a guest
Apr 4th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Gumtree Filter
  3. // @namespace    garet.to.koks.pl
  4. // @version      0.1
  5. // @description  filtr wynikow zapytan gumtree
  6. // @author       Bartek Pawlowski, Garet
  7. // @match        www.gumtree.pl/*
  8. // @grant GM_setValue
  9. // @grant GM_getValue
  10. // @require http://userscripts-mirror.org/scripts/source/107941.user.js
  11. // ==/UserScript==
  12. var contextURL = window.location.pathname;
  13. var contexts = new String(contextURL.split('/'));
  14. contexts = contexts.substring(1, contexts.length).split(',');
  15.  
  16. console.log('contextURL: ' + contextURL);
  17. console.log('contexts: ' + contexts);
  18.  
  19. if (contexts.length == 0) {
  20.     return;
  21. }
  22. var blackListCategory = contexts[0];
  23.  
  24.  
  25. if (!blackListCategory) {
  26.     return;
  27. }
  28.  
  29. console.log('currentCategory: ' + blackListCategory);
  30.  
  31. var blackList = GM_SuperValue.get(blackListCategory);
  32. if (blackList === null || blackList === undefined) {
  33.     blackList = [];
  34. }
  35.  
  36. GM_SuperValue.set(blackListCategory, blackList);
  37.  
  38. console.log('Current blackListed items: ');
  39. console.log(blackList);
  40.  
  41. function removeElementFromArray(inputArray, element){
  42.     var elementIndex = inputArray.indexOf(element);
  43.     if(elementIndex > -1){
  44.         inputArray.splice(elementIndex, 1);
  45.     }
  46. };
  47.  
  48. function arrayContainsElement(inputArray, element) {
  49.     for (var i = 0; i < inputArray.length; i++) {
  50.         var arrayElem = inputArray[i];
  51.         if (element.indexOf(arrayElem) > -1 || arrayElem.indexOf(element) > -1) {
  52.             return true;
  53.         }
  54.     }
  55.  
  56.     return false;
  57. };
  58.  
  59. function clearBlackList(blackListCategory) {
  60.     blackList = [];
  61.     GM_SuperValue.set(blackListCategory, blackList);
  62. }
  63.  
  64. function createButton(buttonName, onClickListener) {
  65.     var button = document.createElement('BUTTON');
  66.     button.appendChild(document.createTextNode(buttonName));
  67.     button.onclick = onClickListener;
  68.     button.href = "";
  69.  
  70.     return button;
  71. };
  72.  
  73. function getHyperLinkFromElement(element) {
  74.     var potentialLinkElements = element.getElementsByClassName('adLinkSB');
  75.     var linkElement = potentialLinkElements[0];
  76.  
  77.     if (linkElement !== null && linkElement !== undefined) {
  78.         return linkElement.href;
  79.     } else {
  80.         return null;
  81.     }
  82. };
  83.  
  84. function addBlackListButton(element) {
  85.     if (element !== null && element !== undefined) {
  86.         element.appendChild(createButton('czarnolisto!', function () {
  87.             var hyperLink = getHyperLinkFromElement(element);
  88.             //dodajemy do czarnolisto
  89.             addElementToBlackList(hyperLink);
  90.  
  91.             //usuwamy z DOMa
  92.             element.parentElement.removeChild(element);
  93.         }));
  94.     }
  95. };
  96.  
  97. function addElementToBlackList(element){
  98.     blackList.push(element);
  99.     GM_SuperValue.set(blackListCategory, blackList);
  100.     if(blackListWindow != null){
  101.         blackListWindow.onBlackListItemAdded(element);
  102.     }
  103. };
  104.  
  105. function removeElementFromBlackList(element){
  106.     removeElementFromArray(blackList, element);
  107.     GM_SuperValue.set(blackListCategory, blackList);
  108. };
  109.  
  110. function addBlackListButtons() {
  111.     var imageContainers = document.getElementsByClassName('ar-image');
  112.  
  113.     for (var i = 0; i < imageContainers.length; i++) {
  114.         var imageContainer = imageContainers[i];
  115.         addBlackListButton(imageContainer.parentElement.parentElement);
  116.     }
  117. };
  118.  
  119. function checkIfShouldBeRemoved(parent) {
  120.     var potentialLinkElements = parent.getElementsByClassName('adLinkSB');
  121.     var linkElement = potentialLinkElements[0];
  122.  
  123.     if (linkElement !== null && linkElement !== undefined) {
  124.         return arrayContainsElement(blackList, linkElement.href);
  125.     }
  126.  
  127.     return false;
  128. };
  129.  
  130. //Matchuje elementy z DOMa z blackLista
  131. function getBlackListedEntries() {
  132.     var resultSet = document.getElementsByClassName('resultsTableSB');
  133.  
  134.     var childrenToRemove = [];
  135.     for (var i = 0; i < resultSet.length; i++) {
  136.         var resultContainer = resultSet[i];
  137.         if (checkIfShouldBeRemoved(resultContainer)) {
  138.             childrenToRemove.push(resultContainer);
  139.         }
  140.     }
  141.  
  142.     return childrenToRemove;
  143. };
  144.  
  145. //usuwa dodane do czarnej listy elementy
  146. function removeBlackListedEntries(childrenToRemove) {
  147.     for (var i = 0; i < childrenToRemove.length; i++) {
  148.         var element = childrenToRemove[i];
  149.         element.parentElement.removeChild(element);
  150.     }
  151. };
  152.  
  153. function clearAds() {
  154.     //usuniecie gornej reklamy adsense
  155.     var adSenseContent = document.getElementById('topAdSense');
  156.     adSenseContent.parentElement.removeChild(adSenseContent);
  157.  
  158.     //usuniecie dolnej reklamy adsense
  159.     adSenseContent = document.getElementById('bottomAdSense');
  160.     adSenseContent.parentElement.removeChild(adSenseContent);
  161. };
  162.  
  163. var blackListWindow;
  164.  
  165. function openBlackListWindow() {
  166.     blackListWindow = window.open('', '', 'width=450, height=300');
  167.     blackListWindow.document.title = 'Czarnolisto dla: ' + blackListCategory;
  168.     blackListWindow.onbeforeunload  = function(){
  169.         blackListWindow = null;
  170.         alert('Pamietaj aby przeladowac strone!');
  171.     };
  172.  
  173.     blackListWindow.scrollableContainer = blackListWindow.document.createElement('div');
  174.     blackListWindow.scrollableContainer.overflowY = 'scrollable';
  175.     blackListWindow.document.body.appendChild(blackListWindow.scrollableContainer);
  176.  
  177.     blackListWindow.onBlackListItemAdded = function(blackListedURL){
  178.         blackListWindow.addBlackListItem(blackListedURL);
  179.     };
  180.  
  181.     blackListWindow.addBlackListItem = function(blackListItem){
  182.         var blackListItemContainer = document.createElement('div');
  183.         blackListItemContainer.id=blackListItem;
  184.         var button = createButton('X', null);
  185.  
  186.         button.onclick = function (mouseEvent) {
  187.             var srcButton = mouseEvent.srcElement;
  188.             removeElementFromBlackList(srcButton.valueContainer.blackListItem);
  189.             blackListWindow.scrollableContainer.removeChild(srcButton.valueContainer.blackListItemContainer);
  190.         };
  191.         button.valueContainer = {
  192.             blackListItem: blackListItem,
  193.             blackListItemContainer: blackListItemContainer
  194.         };
  195.  
  196.         button.style.cssFloat = 'left';
  197.         button.style.marginRight = "15px";
  198.  
  199.         var textDiv = blackListWindow.document.createElement('div');
  200.         textDiv.innerHTML = blackListItem;
  201.         textDiv.overflowX = 'hidden';
  202.         textDiv.title = blackListItem;
  203.         textDiv.style.maxWidth = '350px';
  204.         textDiv.style.whiteSpace = 'nowrap';
  205.  
  206.         blackListItemContainer.appendChild(button);
  207.         blackListItemContainer.appendChild(textDiv);
  208.         blackListItemContainer.appendChild(blackListWindow.document.createElement('br'));
  209.  
  210.         blackListWindow.scrollableContainer.appendChild(blackListItemContainer);
  211.     };
  212.  
  213.     var currBlackList = GM_SuperValue.get(blackListCategory);
  214.  
  215.     for (var i = 0; i < currBlackList.length; i++) {
  216.         var blackListItem = currBlackList[i];
  217.         blackListWindow.addBlackListItem(blackListItem);
  218.     }
  219.  
  220. };
  221.  
  222. function addOpenBlackListWindowButton() {
  223.     var manageBlackListButton = createButton('pokaz czarnolisto', function () {
  224.         openBlackListWindow();
  225.     });
  226.  
  227.     var mainContainer = document.getElementById("main");
  228.     mainContainer.insertBefore(manageBlackListButton, mainContainer.children[0]);
  229. };
  230.  
  231.  
  232. $(document).ready(function () {
  233. //    alert('Skrypt filtrowania jest uruchomiony. Wyniki na stronie sÄ… filtrowane!');
  234.  
  235.     //czysci reklamy adsense
  236.     clearAds();
  237.  
  238.     //dodaje przuciski "czarnolisto"
  239.     addBlackListButtons();
  240.  
  241.     //dodaje przycisk "pokaz czarnolisto"
  242.     addOpenBlackListWindowButton();
  243.  
  244.     //poszukuje elementow z DOMa w czarnolisto i zwraca je tutaj
  245.     var childrenToRemove = getBlackListedEntries();
  246.  
  247.     //usuniecie z DOMa wszystkich odnalezionych elementow w czarnolisto
  248.     removeBlackListedEntries(childrenToRemove);
  249. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement