Skip to content

Instantly share code, notes, and snippets.

@satinka
Last active March 6, 2019 00:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save satinka/5479a93d389a07d41246 to your computer and use it in GitHub Desktop.
Save satinka/5479a93d389a07d41246 to your computer and use it in GitHub Desktop.
added detailed localization to Shutterstock Latest Downloads map
// ==UserScript==
// @name ShutterPlace
// @namespace
// @version 1.0
// @description added detailed localization to Shutterstock Latest Downloads map
// @author Satinka
// @match http://submit.shutterstock.com/home.mhtml*
// @match https://submit.shutterstock.com/home.mhtml*
// @copyright 2016, Satinka
// @require http://code.jquery.com/jquery-latest.min.js
// @grant none
// ==/UserScript==
/* jshint -W097 */
'use strict';
// =========== PARAMETERS ===========
var fontColor = 'brown';
var fontSize = '12pt';
var fontWeight = 'bold'; //'normal';
var showUnknownLocation = true; // shows the title "Unknown location" in case coordinates are not defined (true), or empty title (false)
var useShortCountryName = true; // US (true), or United States of America (false)
//===================================
var $j = jQuery.noConflict();
var arrLocations = [];
$j(document).ready(function() {
CreateStyles();
GetCoords();
window.setInterval(AddToMap, 1000);
});
function GetCoords() {
$j.ajax({
url: window.location.protocol + '//submit.shutterstock.com/show_component.mhtml?component_path=download_map%2Frecent_downloads.mh',
type: "get",
dataType: "html",
error: function (request, status, error) {
//alert(request.responseText);
},
success: function( data ){
var coords = $j.parseJSON(data);
GetLocations(coords);
}
});
}
function GetLocations(coords) {
$j.each(coords, function( ind, el ) {
var id = el.media_id;
if (el.latitude !== null && el.longitude !== null) {
$j.ajax({
url: 'https://maps.googleapis.com/maps/api/geocode/json?latlng=' + el.latitude + ',' + el.longitude,
type: "get",
dataType: "html",
error: function (request, status, error) {
//alert(request.responseText);
},
success: function( data ){
var res = $j.parseJSON(data);
if (res.status == "OK") {
var loc = ExtractLocation(res.results[0].address_components);
arrLocations.push({index: ind, id: id, location: loc});
}
}
});
}
else {
var loc = showUnknownLocation ? "Unknown location" : "";
arrLocations.push({index: ind, id: id, location: loc});
}
});
}
function ExtractLocation(details) {
var loc = "";
var country = "";
var locality = "";
var admin_area1 = "";
var admin_area2 = "";
$j.each(details, function( ind, el ) {
if ($j.inArray("country", el.types) != -1)
country = useShortCountryName ? el.short_name : el.long_name;
if ($j.inArray("locality", el.types) != -1)
locality = el.long_name;
if ($j.inArray("administrative_area_level_1", el.types) != -1)
admin_area1 = el.short_name;
if ($j.inArray("administrative_area_level_2", el.types) != -1)
admin_area2 = el.long_name;
});
loc = loc + ((locality !== "") ? locality + ", " : "") +
//((admin_area2 != "") ? admin_area2 + ", " : "") +
//((admin_area1 != "") ? admin_area1 + ", " : "") +
((country !== "") ? country : "");
return loc;
}
function AddToMap() {
sortByKey(arrLocations, "index");
for (var i = arrLocations.length - 1; i >= 0; i--) {
var el = arrLocations[i];
var title = "<div class='title'>" + el.location + "</div>";
var imgs = $j("img[src$='" + el.id + ".jpg']");
$j.each(imgs, function( ind, img ) {
if ($j(img).closest("div").find('.title').length === 0)
{
$j(img).closest("div").prepend(title);
arrLocations.splice(i, 1);
return false;
}
});
}
}
function CreateStyles() {
var sheet = (function() {
var style = document.createElement("style");
style.appendChild(document.createTextNode(""));
document.head.appendChild(style);
return style.sheet;
})();
var titleStyle = "position: absolute; top: -20px; width: 300px;" +
"text-shadow: 0 0 5px #fff; text-align: left;" +
"font-weight: " + fontWeight + ";" +
"color: " + fontColor + ";" +
"font-size: " + fontSize + ";";
addCSSRule(sheet, ".title", titleStyle, 0);
}
function addCSSRule(sheet, selector, rules, index) {
if("insertRule" in sheet) {
sheet.insertRule(selector + "{" + rules + "}", index);
}
else if("addRule" in sheet) {
sheet.addRule(selector, rules, index);
}
}
function sortByKey(array, key) {
return array.sort(function(a, b) {
var x = a[key]; var y = b[key];
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment