// ==UserScript==
// @name Stig's Flickr Fixr
// @namespace dk.rockland.userscript.flickr.fixr
// @description Display and update photo-notes, Show photographer's albums on photostream-pages, Increase display-size and quality of "old" photos, Photographer's other photos by tag-links, Links to album-map and album-comments, Actually show a geotagged photo on the associated map, Top-pager - And more to come?...
// @author Stig Nygaard, http://www.rockland.dk, https://www.flickr.com/photos/stignygaard/
// @homepageURL http://www.rockland.dk/userscript/flickr/fixr/
// @supportURL http://www.rockland.dk/userscript/flickr/fixr/
// @icon http://www.rockland.dk/img/fixr32.png
// @icon64 http://www.rockland.dk/img/fixr64.png
// @include https://www.flickr.com/*
// @include https://flickr.com/*
// @match https://*.flickr.com/*
// @version 2016.02.07.2
// @grant none
// @run-at document-start
// @downloadURL none
// ==/UserScript==
// CHANGELOG - The most important updates/versions:
var changelog = [
{version: '2016.02.06.2', description: 'New feature: Top-pager! Hover the mouse in the center just above photostream to show a pagination-bar.'},
{version: '2016.02.05.1', description: 'Adding a little "bling" to the album-column.'},
{version: '2016.01.30.0', description: 'Killing the terrible annoying sign-up box that keeps popping up if you are *not* logged in to Flickr. Also fixes for and fine-tuning of the notes-support.'},
{version: '2016.01.24.5', description: '[2016.01.26] Alignment adjustment to "Add Note"-icon (It was not always reachable/clickable).'},
{version: '2016.01.24.3', description: 'New feature: Updating notes on photos! Besides displaying, you can now also Create, Edit and Delete notes (in a "hacky" and slightly restricted but generally usable way)'},
{version: '2015.12.05.2', description: 'Photo-notes support now includes displaying active links and formatting.'},
{version: '2015.12.03.2', description: 'New feature: Basic "beta" support for the good old photo-notes (read-only, public and safe photos only, no formatting/links).'},
{version: '2015.11.28.1', description: 'New feature: Album-headers are now updated with links to album-map and album-comments.'},
{version: '2015.08.26.4', description: 'Initial release version.'}
];
var DEBUG = false;
function log(s) {
if (DEBUG && window.console) {
window.console.log(s);
}
}
if (DEBUG) {
if ('loading' === document.readyState) {
log("This userscript is running at document-start time.");
} else {
log("This userscript is running with document.readyState: " + document.readyState);
}
window.addEventListener('DOMContentLoaded', function(){log('(onDOMContentLoaded)');}, false);
window.addEventListener('focus', function(){log('(onfocus)');}, false);
window.addEventListener('load', function(){log('(onload)');}, false);
window.addEventListener('pageshow', function(){log('(onpageshow)');}, false);
window.addEventListener('resize', function(){log('(onresize)');}, false);
window.addEventListener('hashchange', function(){log('(onhashchange)');}, false);
window.addEventListener('blur', function(){log('(onblur)');}, false);
}
var allowUpdateNotesViaAPIExplorer = true; // Allow support for Create, Edit and Delete photo-notes using Flickr's API Explorer
// FIXR page-tracker
var fixr = {
context: {
pageType: '',
pageSubType: '',
userId: '',
photographerId: '', // value might be delayed (If uninitialized, try call initPhotographerId())
photographerIcon: '',
photographerAlias: '', // (pathalias) bonus-info sometimes initialized (from url) when initializing photoId or albumId
photographerName: '',
photoId: '',
albumId: '',
groupId: '',
galleryId: ''
},
content: null,
pageactionsCount: 0,
timerResizeActionDelayed: 0,
onPageHandlers: [],
onResizeHandlers: [],
onFocusHandlers: [],
runningDirty: function() { // In-development and extra experiments enabled?
return (DEBUG && (fixr.context.userId==='10259776@N00'));
},
initUserId: function () {
if (window.auth && window.auth.user && window.auth.user.nsid) {
fixr.context.userId = window.auth.user.nsid;
return true;
}
return false;
},
initPhotographerId: function () { // photographer/attribution id
var elem;
if (document.querySelector('div.photostream-page-view')) {
// photostream
elem = document.querySelector('div.photostream-page-view div.fluid-photostream-coverphoto-view div.avatar.person');
} else if (document.querySelector('div.photo-page-scrappy-view')) {
// photopage
elem = document.querySelector('div.photo-page-scrappy-view div.sub-photo-view div.avatar.person');
} else if (document.querySelector('div.photo-page-lightbox-scrappy-view')) {
// photopage lightbox
elem = document.querySelector('div.photo-page-lightbox-scrappy-view div.photo-well-view div.photo-attribution div.avatar.person');
} else if (document.querySelector('div.album-page-view')) {
// album page
elem = document.querySelector('div.album-page-view div.album-container div.album-header-view div.album-attribution div.avatar.person');
} else {
log('we do not look for photographerId on this page');
return true;
}
// album oversigt
// etc...
// men minus f.eks. favorites oversigt!
if (!elem) {
log('fixr.initPhotographerId() - Attribution elem NOT found - returning false');
return false;
} // re-run a little later???
log('fixr.initPhotographerId() - Attribution elem found');
// (div.avatar.person).style.backgroundImage=url(https://s.yimg.com/pw/images/buddyicon07_r.png#44504567@N00)
// .style.backgroundImage=url(//c4.staticflickr.com/8/7355/buddyicons/10259776@N00_r.jpg?1372021232#10259776@N00)
if (elem.style.backgroundImage) {
log('fixr.initPhotographerId() - elem has style.backgroundImage "' + elem.style.backgroundImage + '", now looking for the attribution id...');
var pattern = /url[^#\?]+(\/\/[^#\?]+\.com\/[^#\?]+\/buddyicon[^\?\#]+)[^#]*#(\d+\@N\d{2})/i;
// var pattern = /\/buddyicons\/(\d+\@N\d{2})\D+/i;
var result = elem.style.backgroundImage.match(pattern);
if (result) {
log('fixr.initPhotographerId() - Attribution pattern match found: ' + result[0]);
log('fixr.initPhotographerId() - the attribution icon is ' + result[1]);
log('fixr.initPhotographerId() - the attribution id is ' + result[2]);
fixr.context.photographerIcon = result[1];
fixr.context.photographerId = result[2];
} else {
log('fixr.initPhotographerId() - attribution pattern match not found');
return false;
}
} else {
log('fixr.initPhotographerId() - elem.style.backgroundImage not found');
return false;
}
log('fixr.initPhotographerId() - returning true...');
return true;
},
initPhotoId: function () { // Photo Id
// *flickr.com/photos/user/PId/*
var pattern = /^\/photos\/([^\/]+)\/([\d]{2,})/i;
var result = window.location.pathname.match(pattern);
if (result) {
log('url match med photoId=' + result[2]);
log('url match med photographerAlias=' + result[1]);
fixr.context.photoId = result[2];
fixr.context.photographerAlias = result[1];
return true;
}
return false;
},
initAlbumId: function () {
// *flickr.com/photos/user/albums/AId/*
// *flickr.com/photos/user/sets/AId/*
var pattern = /^\/photos\/([^\/]+)\/albums\/([\d]{2,})/i;
var result = window.location.pathname.match(pattern);
if (!result) {
pattern = /^\/photos\/([^\/]+)\/sets\/([\d]{2,})/i;
result = window.location.pathname.match(pattern);
}
if (result) {
log('url match med albumId=' + result[2]);
log('url match med photographerAlias=' + result[1]);
fixr.context.albumId = result[2];
fixr.context.photographerAlias = result[1];
return true;
}
return false;
},
pageActions: function () {
if (fixr.content) {
log('fixr.pageActions() has started with fixr.content defined');
} else {
log('fixr.pageActions() was called, but fixr.content NOT defined');
return;
}
fixr.pageactionsCount++;
for (var p in fixr.context) { // reset context on new page
if (fixr.context.hasOwnProperty(p)) {
fixr.context[p] = '';
}
}
if (fixr.content.querySelector('div.photostream-page-view')) {
if (fixr.content.querySelector('div.slideshow-view')) {
fixr.context.pageType = 'PHOTOSTREAM SLIDESHOW';
} else {
fixr.context.pageType = 'PHOTOSTREAM';
}
} else if (fixr.content.querySelector('div.photo-page-scrappy-view')) {
fixr.context.pageType = 'PHOTOPAGE';
if (fixr.content.querySelector('div.vr-overlay-view') && fixr.content.querySelector('div.vr-overlay-view').hasChildNodes()) {
fixr.context.pageSubType = 'VR'; // maybe I can find a better way to detect, not sure how reliable this is?
} else if (fixr.content.querySelector('div.videoplayer')) {
fixr.context.pageSubType='VIDEO';
} else {
fixr.context.pageSubType='PHOTO';
}
} else if (fixr.content.querySelector('div.photo-page-lightbox-scrappy-view')) {
fixr.context.pageType = 'PHOTOPAGE LIGHTBOX';
if (fixr.content.querySelector('div.vr-overlay-view') && fixr.content.querySelector('div.vr-overlay-view').hasChildNodes()) {
fixr.context.pageSubType='VR'; // VR-mode currently not supported in lightbox?
} else if (fixr.content.querySelector('div.videoplayer')) {
fixr.context.pageSubType='VIDEO';
} else {
fixr.context.pageSubType='PHOTO';
}
} else if (fixr.content.querySelector('div.albums-list-page-view')) {
fixr.context.pageType = 'ALBUMSLIST';
} else if (fixr.content.querySelector('div.album-page-view')) {
if (fixr.content.querySelector('div.slideshow-view')) {
fixr.context.pageType = 'ALBUM SLIDESHOW';
} else {
fixr.context.pageType = 'ALBUM';
}
} else if (fixr.content.querySelector('div.cameraroll-page-view')) {
fixr.context.pageType = 'CAMERAROLL';
} else if (fixr.content.querySelector('div.explore-page-view')) {
fixr.context.pageType = 'EXPLORE';
} else if (fixr.content.querySelector('div.favorites-page-view')) {
if (fixr.content.querySelector('div.slideshow-view')) {
fixr.context.pageType = 'FAVORITES SLIDESHOW';
} else {
fixr.context.pageType = 'FAVORITES';
}
} else if (fixr.content.querySelector('div.groups-list-view')) {
fixr.context.pageType = 'GROUPSLIST'; // personal grouplist
} else if (fixr.content.querySelector('div#activityFeed')) { // id=main i stedet for id=fixr.content
fixr.context.pageType = 'ACTIVITYFEED'; // aka. front page
} else {
// fixr.context.pageType = ''; // unknown
}
log('fixr.context.pageType = ' + fixr.context.pageType);
log('fixr.context.pageSubType = '+fixr.context.pageSubType);
if (fixr.initUserId()) {
log('fixr.initUserId() returned with succes: '+fixr.context.userId);
} else {
log('fixr.initUserId() returned FALSE!');
}
if (fixr.initPhotographerId()) {
log('fixr.initPhotographerId() returned true');
} else {
log('fixr.initPhotographerId() returned false - re-running delayed...');
setTimeout(fixr.initPhotographerId, 2000);
}
if (fixr.initPhotoId()) {
log('fixr.initPhotoId() returned true');
} else {
log('fixr.initPhotoId() returned false');
}
if (fixr.initAlbumId()) {
log('fixr.initAlbumId() returned true');
}
if (fixr.content.querySelector('a.owner-name')) {
fixr.context.photographerName = fixr.content.querySelector('a.owner-name').textContent;
}
// Now run the page handlers....
if (fixr.onPageHandlers && fixr.onPageHandlers !== null && fixr.onPageHandlers.length) {
log('We have ' + fixr.onPageHandlers.length + ' onPage handlers starting now...');
for (var f = 0; f < fixr.onPageHandlers.length; f++) {
fixr.onPageHandlers[f]();
}
}
},
setupContent: function () {
if (document.getElementById('content')) {
fixr.content = document.getElementById('content');
} else if (document.getElementById('main')) {
fixr.content = document.getElementById('main'); // frontpage
}
if (fixr.content && fixr.content.id) {
log('fixr.content.id = ' + fixr.content.id);
} else {
log('content or main element NOT found!');
}
},
runPageActionsIfMissed: function () {
if (fixr.pageactionsCount === 0) {
log('Vi kører fixr.pageActions() på bagkant via onload...');
fixr.setupContent();
if (fixr.content === null) {
log('Vi kan IKKE køre fixr.pageActions() på bagkant, da fixr.content ikke er defineret');
return;
}
fixr.pageActions();
} else {
log('ej nødvendigt at køre fixr.pageActions() på bagkant i dette tilfælde...');
}
},
runDelayedPageActionsIfMissed: function () {
setTimeout(fixr.runPageActionsIfMissed, 2000);
},
resizeActions: function () {
if (fixr.onResizeHandlers && fixr.onResizeHandlers !== null && fixr.onResizeHandlers.length) {
for (var f = 0; f < fixr.onResizeHandlers.length; f++) {
fixr.onResizeHandlers[f]();
}
}
},
resizeActionsDelayed: function () { // or "preburner"
clearTimeout(fixr.timerResizeActionDelayed);
fixr.timerResizeActionDelayed = setTimeout(fixr.resizeActions, 250);
},
focusActions: function () {
if (fixr.onFocusHandlers && fixr.onFocusHandlers !== null && fixr.onFocusHandlers.length) {
for (var f = 0; f < fixr.onFocusHandlers.length; f++) {
fixr.onFocusHandlers[f]();
}
}
},
setupObserver: function () {
log('fixr.setupObserve INITIALIZATION START');
fixr.setupContent();
if (fixr.content === null) {
log('Init fails because content not defined');
return;
}
// create an observer instance
var observer = new MutationObserver(function (mutations) {
log('NEW PAGE MUTATION!');
//mutations.forEach(function(mutation) {
// log('MO: '+mutation.type); // might check for specific type of "mutations" (MutationRecord)
//});
fixr.pageActions();
}); // MutationObserver end
// configuration of the observer:
var config = {attributes: false, childList: true, subtree: false, characterData: false};
observer.observe(fixr.content, config);
log('fixr.setupObserve INITIALIZATION DONE');
},
init: function (onPageHandlerArray, onResizeHandlerArray, onFocusHandlerArray) {
// General page-change observer setup:
window.addEventListener('DOMContentLoaded', fixr.setupObserver, false); // Page on DOMContentLoaded
window.addEventListener('load', fixr.runDelayedPageActionsIfMissed, false); // Page on load
window.addEventListener('resize', fixr.resizeActionsDelayed, false); // også på resize
window.addEventListener('focus', fixr.focusActions, false);
if (onPageHandlerArray && onPageHandlerArray !== null && onPageHandlerArray.length) {
fixr.onPageHandlers = onPageHandlerArray; // Replace by adding with a one-by-one by "helper" for flexibility?
}
if (onResizeHandlerArray && onResizeHandlerArray !== null && onResizeHandlerArray.length) {
fixr.onResizeHandlers = onResizeHandlerArray; // Replace by adding with a one-by-one by "helper" for flexibility?
}
if (onFocusHandlerArray && onFocusHandlerArray !== null && onFocusHandlerArray.length) {
fixr.onFocusHandlers = onFocusHandlerArray;
}
}
};
// FIXR page-tracker end
var _timerMaplink = 0;
function updateMapLink() {
if (fixr.context.pageType !== 'PHOTOPAGE') {
return; // exit if not photopage
}
log('updateMapLink() running at readystate=' + document.readyState + ' and with photoId=' + fixr.context.photoId);
if (fixr.context.photoId) {
var maplink = fixr.content.querySelector('a.static-maps');
if (maplink) {
if (maplink.getAttribute('href') && (maplink.getAttribute('href').indexOf('map/?') > 0) && (maplink.getAttribute('href').indexOf('&photo=') === -1)) {
maplink.setAttribute('href', maplink.getAttribute('href') + '&photo=' + fixr.context.photoId);
log('link is updated by updateMapLink() at readystate=' + document.readyState);
} else {
log('link NOT updated by updateMapLink(). Invalid element or already updated. readystate=' + document.readyState);
}
} else {
log('NO maplink found at readystate=' + document.readyState + '. Re-try later?');
}
} else {
log('NO photoId found at readystate=' + document.readyState);
}
}
function updateMapLinkDelayed() {
if (fixr.context.pageType !== 'PHOTOPAGE') {
return;
} // exit if not photopage
log('updateMapLinkDelayed() running... with pageType=' + fixr.context.pageType);
//clearTimeout(_timerMaplink);
_timerMaplink = setTimeout(updateMapLink, 2000); // make maplink work better on photopage
}
var album = { // cache to avoid repeating requests
albumId: '',
commentCount: 0
};
function updateAlbumCommentCount() {
var _reqAlbumComments = null;
if (window.XMLHttpRequest) {
_reqAlbumComments = new XMLHttpRequest();
if (typeof _reqAlbumComments.overrideMimeType !== 'undefined') {
_reqAlbumComments.overrideMimeType('text/html');
}
_reqAlbumComments.onreadystatechange = function () {
if (_reqAlbumComments.readyState === 4 && _reqAlbumComments.status === 200) {
log('_reqAlbumComments returned status=' + _reqAlbumComments.status);
var doc = document.implementation.createHTMLDocument("sizeDoc");
doc.documentElement.innerHTML = _reqAlbumComments.responseText;
album.albumId = fixr.context.albumId;
album.commentCount = -1;
var e = doc.body.querySelectorAll('span.LinksNew b.Here');
if (e && e.length === 1) {
var n = parseInt(e[0].textContent, 10);
if (isNaN(n)) {
album.commentCount = 0;
} else {
album.commentCount = n;
}
} else {
album.commentCount = -1;
log('b.Here??? ');
}
if (document.getElementById('albumCommentCount')) {
if (album.commentCount === -1) {
document.getElementById('albumCommentCount').innerHTML = '?';
} else {
document.getElementById('albumCommentCount').innerHTML = '' + album.commentCount;
}
} else {
log('albumCommentCount element not found');
}
} else {
// wait for the call to complete
}
};
if (fixr.context.albumId === album.albumId && fixr.context.albumId !== '' && album.commentCount !== -1) {
log('Usinging CACHED album count!...');
document.getElementById('albumCommentCount').innerHTML = '' + album.commentCount;
} else if (fixr.context.albumId !== '') {
var url = 'https://www.flickr.com/photos/' + (fixr.context.photographerAlias !== '' ? fixr.context.photographerAlias : fixr.context.photographerId) + '/albums/' + fixr.context.albumId + '/comments/';
_reqAlbumComments.open('GET', url, true);
_reqAlbumComments.send(null);
} else {
log('albumId not initialized');
}
} else {
log('understøtter ikke XMLHttpRequest');
}
}
var albums = { // cache albums to avoid repeating requests
ownerId: '',
html: '',
count: 0
};
function getAlbumlist() {
var _reqAlbumlist = null;
if (window.XMLHttpRequest) {
_reqAlbumlist = new XMLHttpRequest();
if (typeof _reqAlbumlist.overrideMimeType !== 'undefined') {
_reqAlbumlist.overrideMimeType('text/html');
}
_reqAlbumlist.onreadystatechange = function () {
if (_reqAlbumlist.readyState === 4 && _reqAlbumlist.status === 200) {
log('_reqAlbumlist returned status=' + _reqAlbumlist.status); // + ', \ntext:\n' + _reqAlbumlist.responseText);
var doc = document.implementation.createHTMLDocument("sizeDoc");
doc.documentElement.innerHTML = _reqAlbumlist.responseText;
albums.ownerId = fixr.context.photographerId;
albums.html = '';
albums.count = 0;
var e = doc.body.querySelectorAll('div.photo-list-album-view');
var imgPattern = /url\([\'\"]*([^\)\'\"]+)(\.[jpgtifn]{3,4})[\'\"]*\)/i;
if (e && e.length > 0) {
albums.count = e.length;
for (var i = 0; i < Math.min(10, e.length); i++) {
var imgUrl = '';
log(e[i].outerHTML);
log('A7 (' + i + ') : ' + e[i].style.backgroundImage);
// var result = e[i].style.backgroundImage.match(imgPattern); // strangely not working in Chrome
var result = (e[i].outerHTML).match(imgPattern); // quick work-around for above (works for now)
if (result) {
// imgUrl = result[1].replace(/_[a-z]$/, '') + '_s' + result[2];
imgUrl = result[1].replace(/_[a-z]$/, '') + '_q' + result[2];
log('imgUrl=' + imgUrl);
} else {
log('No match on imgPattern');
}
var a = e[i].querySelector('a[href][title]'); // sub-element
if (a && a !== null) {
log('Album title: ' + a.title);
log('Album url: ' + a.getAttribute('href'));
albums.html += '
';
div.className = 'ncontent';
div.style.top = Math.floor(parseInt(n.h, 10) * scalefactor) - 2 + 'px';
div.style.left = '2px';
div.id = 'notetext'+i;
if (allowUpdateNotesViaAPIExplorer) {
// For public&safe photos where notes can be read: Create notes on (almost?) any photos, delete any note on own photos(?), edit own notes...
if (fixr.context.userId) {
// allow create note (however not from here!)
}
if (fixr.context.photographerId===fixr.context.userId) {
// allow delete any note on own photos? (to-do)
}
if (fixr.context.userId===n.author) {
div.innerHTML += '