Last active
March 12, 2018 19:01
-
-
Save Citizen01/2ea71bc8c698ea819850e27f98020499 to your computer and use it in GitHub Desktop.
Script to enhance UX on driiveme.com website
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Driiveme.com assistant | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description For now only adds the model name and the 1st image of the vehicle from google in the listing view. | |
// @author Civilian | |
// @match https://www.driiveme.com/* | |
// @grant GM_xmlhttpRequest | |
// @connect google.fr | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
var offers = $("#list-trajet div.block-trajet"); | |
if (offers) { | |
offers.each((k, offer) => { | |
loadAndShowModel($(offer)); | |
}); | |
} | |
function loadAndShowModel(offer) { | |
var modelSelector = '#details-trajet > div.section.section-grayLight-pattern.section-padding > div > div.row > div.col-lg-8.col-md-7 > div > div.row.aligncenter > div:nth-child(3) > span:nth-child(9)'; | |
var link = offer.find('a').attr('href'); | |
$.get(link, function(data) { | |
var html = $('<div/>').html(data); | |
var model = html.find(modelSelector).text().trim(); | |
offer.find('.type-content').append($('<div/>').text(model)); | |
if (model && model !== 'NC') loadAndShowImage(offer, model); | |
}); | |
// Make images bigger | |
var typeContent = offer.find('.type-content').first(); | |
typeContent.css('width', '27%'); | |
typeContent.find('img').attr('width', '100%'); | |
} | |
function loadAndShowImage(offer, modelName) { | |
var googleQueryFmt = 'https://www.google.fr/search?biw=950&bih=939&tbs=isz%3Am%2Cic%3Atrans%2Citp%3Aphoto%2Cimgo%3A1&tbm=isch&q={0}'; | |
var settings = { | |
"method": "GET", | |
"url": googleQueryFmt.format(modelName.split(' ').join('+')), | |
"onload": function(data) { | |
var html = $('<div/>').html(data.responseText); | |
var meta = html.find("#rg_s div:first div.rg_meta").first(); | |
var json = JSON.parse(meta.text()); | |
console.log(json.ou); | |
if (json.ou) { | |
var img = offer.find('.type-content img').first(); | |
img.attr('src', json.ou); | |
img.wrap($('<a>', { 'href': settings.url, 'target': '_blank' })); | |
} | |
} | |
}; | |
GM_xmlhttpRequest(settings); | |
} | |
if (!String.prototype.format) { | |
String.prototype.format = function() { | |
var args = arguments; | |
return this.replace(/{(\d+)}/g, function(match, number) { | |
return typeof args[number] != 'undefined' ? args[number] : match; | |
}); | |
}; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment