Last active
August 9, 2020 14:37
-
-
Save kriswebdev/e6a0e9e65ff8c601b87d to your computer and use it in GitHub Desktop.
Better Tripadvisor: Precise ratings, website link, phone, google search (click Raw to install with e.g. Tampermonkey)
This file contains hidden or 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 Better Tripadvisor | |
// @description Precise ratings, no restaurant ads, website link, phone number, google search link | |
// @namespace KrisWebDev | |
// @author KrisWebDev | |
// @include http://www.tripadvisor.tld/* | |
// @include https://www.tripadvisor.tld/* | |
// @version 2020.08 | |
// @grant GM_xmlhttpRequest | |
// @grant GM_addStyle | |
// @require http://code.jquery.com/jquery-2.1.4.min.js | |
// @icon https://static.tacdn.com/favicon.ico | |
// ==/UserScript== | |
this.$ = this.jQuery = jQuery.noConflict(true); | |
GM_addStyle('.ui_icon.google::before { content: "\u2315"; }'); | |
// Restaurants: Remove adds | |
GM_addStyle('div[data-test="SL_list_item"] { display: none; }'); | |
/* PRECISE RATINGS */ | |
var coeff = 5; | |
var result = 0; | |
var sumReviews = 0; | |
var percentReviews = 0; | |
var stop = 0; | |
// Hotels | |
$("label[for^='ReviewRatingFilter_']").parent("li").each(function(index,val) { | |
var irating = $(this).children("span").last().text(); | |
console.log(irating); | |
percentReviews = parseInt(irating.replace(/\s+/g, '')); | |
result += percentReviews*coeff--; | |
sumReviews += percentReviews; | |
}); | |
if (sumReviews) { | |
result = (Math.round(result/sumReviews*10))/10; | |
// console.log("Result="+result); | |
$("span[class*='__overallRating']").css("display","hide"); | |
$("div#ABOUT_TAB").parent().parent().before($("<div>").attr( | |
"style", "font-size: 48px;line-height: 22px;font-weight: 700;color: #078171; padding-bottom: 20px").text(result.toFixed(1))); | |
} | |
// Attractions | |
if (!sumReviews) { | |
stop=0; | |
$(".row_count").each(function(index, val) { | |
if(stop++ == 5) | |
return false; | |
percentReviews = parseInt($(this).text().replace(/[^0-9]/g, '')); | |
result += percentReviews*coeff--; | |
sumReviews += percentReviews; | |
stop++; | |
}); | |
if (sumReviews) { | |
// console.log(sumReviews); | |
result = (Math.round(result/sumReviews*10))/10; | |
console.log("Result="+result); | |
$("span[class*='__overallRating']").css("display","hide"); | |
$("div#btf_wrap").prepend($("<div>").attr( | |
"style", "font-size: 48px;line-height: 22px;font-weight: 700;color: #078171; padding-top: 20px").text(result.toFixed(1))); | |
} | |
} | |
// Attraction Reviews | |
if (!sumReviews) { | |
stop=0; | |
$(".row_num.is-shown-at-tablet").each(function(index, val) { | |
if(stop++ == 5) | |
return false; | |
percentReviews = parseInt($(this).text().replace(/[^0-9]/g, '')); | |
result += percentReviews*coeff--; | |
sumReviews += percentReviews; | |
stop++; | |
}); | |
if (sumReviews) { | |
result = (Math.round(result/sumReviews*10))/10; | |
// console.log("Result="+result); | |
$("span[class*='__overallRating']").css("display","hide"); | |
$("div#REVIEWS").parent().prepend($("<div>").attr( | |
"style", "font-size: 48px;line-height: 22px;font-weight: 700;color: #078171; padding-bottom: 20px").text(result.toFixed(1))); | |
} | |
} | |
if (sumReviews) { | |
result = (Math.round(result/sumReviews*10))/10; | |
// console.log("Result="+result); | |
$("span[class*='__overallRating']").css("display","hide"); | |
$("div#ABOUT_TAB").parent().parent().before($("<div>").attr( | |
"style", "font-size: 48px;line-height: 22px;font-weight: 700;color: #078171; padding-bottom: 20px").text(result.toFixed(1))); | |
} | |
/* LEFT/RIGHT KEYS TO SLIDE PHOTOS */ | |
/* | |
$(document).keyup(function(e) { | |
switch (e.key) { | |
case "ArrowRight": | |
$("div.navigationItem.single-chevron-right").click(); | |
break; | |
case "ArrowLeft": | |
$("div.navigationItem.single-chevron-left").click(); | |
break; | |
} | |
}); | |
*/ | |
/* HELPER FUNCTIONS */ | |
function getHTTP(queryURL, handlerPassArg, handler) { | |
var http; | |
GM_xmlhttpRequest({ | |
method: "GET", | |
url: queryURL, | |
timeout: 15000, | |
headers: { | |
Referer: document.URL | |
}, | |
onreadystatechange: function(http) { | |
if (http.readyState == 4) { | |
if(http.status == 200) { | |
handler(http.responseText, handlerPassArg, queryURL); | |
} | |
http = null; | |
} | |
} | |
}); | |
} | |
/* WEBSITE LINK, PHONE HANDLER */ | |
function appendGeneralInfo(response, jContainer, queryurl) { | |
var websiteURL = null; | |
var phoneNum = null; | |
var genInfoHTML = ""; | |
if(needPhone) | |
if(phoneNum=response.match(/data-attr="phone"\s*value="([0-9 \+\(\)]+)"/)) | |
genInfoHTML+='<div class="blEntry phone"><a class="ui_link" href="tel:'+phoneNum[1].replace(/ /g, '')+'" data-column="1"><span class="ui_icon phone"></span><span class="is-hidden-mobile detail">'+phoneNum[1]+'</span></a></div>'; | |
if(needWebsite) | |
if(websiteURL=response.match(/data-attr="website"\s*value="(https?:\/\/[^"]+)"/)) | |
genInfoHTML+='<div class="is-hidden-mobile blEntry website ui_link " data-column="2" data-trackingkey=""><span class="ui_icon laptop detail"></span><a href="'+websiteURL[1].replace(/[\\\"'\x00-\x1F\x7F-\x9F]/g, '')+'" target="_blank"><span class="detail">Website</span></a></div>'; | |
if (genInfoHTML) | |
jContainer.append(genInfoHTML); | |
} | |
/* LAUNCHERS */ | |
var jBlRow=$("div.headerBL > .blRow"); | |
if (jBlRow.length) { | |
/* GOOGLE SEARCH LINK */ | |
var jHeader=$("h1#HEADING"); | |
if(jHeader.length) { | |
var googleSearchHTML = ""; | |
googleSearchHTML+='<div class="is-hidden-mobile blEntry ui_link " data-column="2" data-trackingkey=""><span class="ui_icon google detail"></span><a href="https://www.google.com/search?ie=utf-8&q='+encodeURIComponent(jHeader.text())+'" target="_blank"><span class="detail">Google</span></a></div>'; | |
jBlRow.append(googleSearchHTML); | |
} | |
/* WEBSITE LINK, PHONE LAUNCHER */ | |
var jUpdateListingAnchor=$("a[href^='/UpdateListing-']"); | |
var blEntryPhone=$(".blEntry.phone"); | |
var blEntryWebsite=$(".blEntry.website"); | |
var needPhone=!blEntryPhone.length;// && !$blEntryPhone.find("a[href^='/UpdateListing-']").length; | |
var needWebsite=!blEntryWebsite.length;// && !blEntryWebsite.find("a[href^='/UpdateListing-']").length; | |
// console.log(needWebsite); | |
if (jUpdateListingAnchor.length && (needPhone || needWebsite)) | |
getHTTP(jUpdateListingAnchor[0].href, jBlRow, appendGeneralInfo); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment