// ==UserScript==
// @name flickrAwardCounter
// @namespace http://www.phazeshift.co.uk/download/
// @description Greasemonkey script to count flickr award images
// @version 2.20
// @include http*://*flickr.com/*photos/*/*
// @include http*://*flickr.com/groups/*/discuss/*
// @include http*://*flickr.com/groups/*/pool/*
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_xmlhttpRequest
// @grant GM_log
// @grant GM_registerMenuCommand
// @license MIT
// @downloadURL none
// ==/UserScript==
/*
About
=====
This is a GreaseMonkey script for Flickr.com More information about GreaseMonkey can be found here: http://diveintogreasemonkey.org/install/what-is-greasemonkey.html
Installation
============
First you need firefox http://mozilla.org/firefox then you need to install GreaseMonkey http://greasemonkey.mozdev.org
Restart your browser then revisit this script. You should now see a button in the top right hand corner, click it to install.
Release Notes
============
v1.0 ???? Counts the comments on the current photo page using javascript
v2.0 ???? Counts the comments for a photo using the flickr API on Group, Pool and Photo pages
v2.1 ???? Updated to be more compatible with other scripts; no longer removes PoolList class from pool page. Fixed reloading of comments when clicking Show all.
v2.2 ???? Allows multiple images to be specified per award, (separated with ';') and allows dupes for certain people to be ignored (separated with ';')
v2.3 ???? Config dialog allows more awards to be added instead of being off the page.
v2.4 ???? Added Sort & Edit options to config. Added Auto update function.
v2.5 ???? Added option to display number favourites.
v2.6 ???? Fixed clear config option
v2.7 ???? Changed search code to find text that isn't in image tags
v2.8 ???? Added option to hide awards with count = 0
v2.9 ???? Fixed multiple URL config option & counting multiple awards in single comment
Added option to save settings online and tidied config page
Fixed version check code
v2.10 ???? Applied Alesas patch to work with the new flickr layout. Thanks to Alesa for the patch and Tambako for letting me know about the new layout.
Added option to display number of galleries.
v2.11 ???? Fixed an issue that caused script version to be checked on every page load when running under some Greasemonkey emulators that don't support storing settings.
Changed SaveCloudSettings to use a post request to prevent errors with long URLs
Added option to export counter settings to a string
Fixed compatibility issue with 'Flickr Group Pool Admin - Warn + Delete' script
Updated Pool display technique to interfere less with other scripts
v2.12 ???? Added Aleas patch for update check exception handler
Reverted change to pool handling as award counts were unreadable
Fixed detection of image url for pool photos after recent flickr change
v2.13 ???? Fixed detection of image id for individual photos after recent flickr change
v2.14 ???? Fixed count display spacing on comment threads
Fixed counter to work with new pool layout
v2.15 ???? Fixed show all on group discussion page
v2.16 ???? Fixed Flickr Urls to support https. Thanks to Dave_O1
Added support for justified pool layout
Added greasemonkey @grant metadata
v2.17 ???? Fixed SSL support
Fixed single photo view
v2.18 ???? Fixed discussion view
v2.19 ???? Fixed retrieval of photo id
v2.20 ???? Fixed retrieval of photo id again, using location instead of document content.
Added @version and @license GM tags. MB 2022-03-17.
Known Issues
============
There may be some incompatibilities with other flickr scripts. Please report them if you find them
I'm using an onClick listener for all interactions with the user page - this means I don't have to register a bunch of functions on unsafeWindow but it will probably be a bit slower.
It also makes parameters a bit more tricky to pass but should cause less interferance with other scripts and causes less problems with the scope of GM functions.
Credits
=======
http://diveintogreasemonkey.org/ for a useful reference to greasemonkey
*/
// ===================================================================
// Config Stuff
var glblConfigAwards = undefined;
var glblAllPhotos = undefined;
var glblCommentsText = undefined;
var glblLastPhotoId = undefined;
var glblPhotoId = undefined;
var glblMode = undefined;
var glblAllowDupesFrom = undefined;
var glblVersion = '2.20';
var glblServerVersion = glblVersion;
var glblLastCheckedVersion = undefined;
var glblScriptUrl = 'http://www.phazeshift.co.uk/files/flickrawardcounter.user.js';
var glblScriptVersionUrl = 'http://www.phazeshift.co.uk/flickrawardcounterversion.php';
var glblCloudSettingsUrl = 'http://www.phazeshift.co.uk/flickrawardcountersettings.php?';
var glblCloudSettingsId = "";
var glblUpdateCheck = true;
var glblFavourites = false;
var glblGalleries = false;
var glblHideZeroCount = false;
var glblApiKey = 'cf7bfd7c92fcbea46bb7bce79b81ead7';
const SinglePhotoMode = 'Single';
const GroupMode = 'Group';
const PoolMode = 'Pool';
const ConfigureLink = '( configure )';
const AwardCounter = 'flickr Award Counter ';
const PoolAwardXPath = "//div[@class='flickraward']";
const AwardInnerXPath = ".//span[@id='flickraward-awardcount-inner']";
const AwardFooterXPath = ".//span[@id='awardcount-awardfooter']";
const AwardToHideXPath = ".//span[@id='flickraward-awardcount-tohide']";
function Save()
{
GM_setValue('awards', CreateSaveAwardStr());
if (glblAllowDupesFrom) {
GM_setValue('awards-allow-dupes-from', glblAllowDupesFrom.join(';').toLowerCase());
}
GM_setValue('favourites',glblFavourites);
GM_setValue('galleries',glblGalleries);
GM_setValue('hidezerocount',glblHideZeroCount);
GM_setValue('updatecheck',glblUpdateCheck);
SaveCloudSettingsId();
}
function SaveCloudSettingsId() {
GM_setValue('cloudsettingsid',glblCloudSettingsId);
}
function SaveUpdateInfo()
{
GM_setValue('serverversion',glblServerVersion);
if (glblLastCheckedVersion) { GM_setValue('lastcheckedversion',glblLastCheckedVersion.getTime().toString()); }
}
function GmSettingsWorking()
{
GM_setValue('gmsettingstest',glblVersion);
return (glblVersion == GM_getValue('gmsettingstest',''));
}
function Load()
{
ParseSaveAwardStr(GM_getValue('awards', ''));
buf = GM_getValue('awards-allow-dupes-from', '');
if (buf.length > 0){
glblAllowDupesFrom = buf.toLowerCase().split(';');
}
glblServerVersion = GM_getValue('serverversion',glblServerVersion);
buf = GM_getValue('lastcheckedversion');
if (buf && buf != ''){
glblLastCheckedVersion = new Date();
glblLastCheckedVersion.setTime(buf);
}
glblUpdateCheck = GM_getValue('updatecheck',glblUpdateCheck);
if (!GmSettingsWorking())
glblUpdateCheck = false;
glblFavourites = GM_getValue('favourites',glblFavourites);
glblGalleries = GM_getValue('galleries',glblGalleries);
glblHideZeroCount = GM_getValue('hidezerocount',glblHideZeroCount);
glblCloudSettingsId = GM_getValue('cloudsettingsid',glblCloudSettingsId);
}
function CreateSaveAwardStr()
{
var buf = "";
for (var i = 0; i < glblConfigAwards.length; i++) {
if (glblConfigAwards[i] != undefined)
{
buf = buf + escape(glblConfigAwards[i]["Name"]) + ',' + escape(glblConfigAwards[i]["Url"].join(';')) + "|";
}
}
buf = buf.substring(0,buf.length-1);
return buf;
}
function ParseSaveAwardStr(buf)
{
glblConfigAwards = new Array();
if (buf == undefined) { return; }
Items = buf.split('|');
for (var i = 0; i < Items.length; i++) {
if (Items[i] != '')
{
Items2 = Items[i].split(',');
try {
AddAward(unescape(Items2[0]),unescape(Items2[1]));
} catch (ex) {
alert(ex)
}
}
}
}
function CmpConfigAwards(a,b)
{
var x = a["Name"].toLowerCase();
var y = b["Name"].toLowerCase();
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
function Sort()
{
glblConfigAwards.sort(CmpConfigAwards);
ConfigDisplayAwards();
}
function SortOne(awardId,up)
{
var i = FindAwardPosByName(awardId);
if (i == -1) {return; }
var tmpAward = glblConfigAwards[i];
if (up) { newPos = i - 1; }
else { newPos = i + 1; }
if ((newPos < 0) || newPos > glblConfigAwards.length - 1) { return; }
glblConfigAwards[i] = glblConfigAwards[newPos];
glblConfigAwards[newPos] = tmpAward;
ConfigDisplayAwards();
}
function FindAwardByName(awardid) {
var i = FindAwardPosByName(awardid);
if (i == -1) return undefined;
return glblConfigAwards[i];
}
function FindAwardPosByName(awardid){
for (i=0; i < glblConfigAwards.length; i++) {
if (glblConfigAwards[i] != undefined)
{
Award = glblConfigAwards[i];
if (Award["Name"] == awardid)
{
return i;
}
}
}
return -1;
}
function ConfigDisplayAwardRow(buf,Award,pos,total)
{
var buf = '
';
return buf;
}
function ConfigDisplayAwards()
{
var varElement = document.getElementById("AwardCounterVarList");
buf = '';
var i;
var awardAdded = false;
for (i=0; i < glblConfigAwards.length; i++) {
if (glblConfigAwards[i]) {
Award = glblConfigAwards[i];
buf += ConfigDisplayAwardRow(buf,Award,i,glblConfigAwards.length-1);
awardAdded = true;
}
}
buf += '';
if (!awardAdded)
{
buf = 'None defined';
}
// Replace html with new stuff
varElement.innerHTML = buf;
}
function DeleteElement(elementname)
{
var varElement = document.getElementById(elementname);
if (varElement) {
varElement.parentNode.removeChild(varElement);
}
}
function SafeSetElementValue(elename, elevalue){
var varElement = document.getElementById(elename);
if (varElement) {
varElement.value = elevalue;
}
}
function trim(stringToTrim) {
return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function TrimArray(arr){
for (i = 0; i -1) return false;
if (Value.indexOf(',') > -1) return false;
return true;
}
function AddAward(Name, Url)
{
var newAward = true;
if (!Name) {return};
if (!Url) {return};
Name = RemoveTags(Name);
var Award = FindAwardByName(Name);
if (!CheckString(Name)) {
throw('Name contains a \'|\' or a \',\' which isn\'t valid');
}
if (!CheckString(Url)) {
throw('Url contains a \'|\' or a \',\' which isn\'t valid');
}
if (Award != undefined) {
newAward = false;
}
if (newAward)
{
Award = {"Name" : "", "Url" : ""};
}
Award["Name"] = Name;
Award["Url"] = TrimArray(Url.split(';'));
if (newAward)
{
glblConfigAwards[glblConfigAwards.length] = Award;
}
}
function EditAward(Name) {
var Award = FindAwardByName(Name);
if (Award == undefined) return;
document.getElementById("AwardName").value = Award["Name"];
document.getElementById("AwardUrl").value = Award["Url"].join(';');
}
function RemoveAward(Name)
{
var i = FindAwardPosByName(Name);
if (i != -1) {
glblConfigAwards[i] = undefined;
}
ConfigDisplayAwards();
}
function NewerVersion(serverVersion, currentVersion){
serverVersion = serverVersion.split(".");
currentVersion = currentVersion.split(".");
var i;
for (i=0;i parseInt(currentVersion[i])) return true;
}
return false;
}
function UpdateHTML()
{
if (glblUpdateCheck) {
var buf = ' Update status: ';
if (NewerVersion(glblServerVersion,glblVersion)) {
buf += ' New version '+ glblServerVersion +' ';
} else buf += 'None available ';
if (glblLastCheckedVersion) buf += ' (Checked: '+ glblLastCheckedVersion.toLocaleDateString() + ' ' + glblLastCheckedVersion.toLocaleTimeString() +')';
buf += '';
return buf;
}
return '';
}
function CreateConfigPage()
{
var buf = "";
buf += '
';
buf += '
flickr Award Counter
';
buf += '
(v'+ glblVersion +') Copyright 2007 Phazeshift. All rights reserved. ';
buf += UpdateHTML();
buf += '
';
buf += '
This is a small GreaseMonkey script to count the number of awards given to photos in groups on flickr. It will count any number of different awards given by users. ';
buf += 'It will ignore duplicate awards of the same type from a user. To start, just add a name for the award below and paste the location of the image file for the award, click Add, then click Save.
';
buf += "You can enter multiple images per award, Separate each item with ';'. Don't include spaces before or after items.
";
buf += '
Allow Duplicates From ';
buf += '
';
buf += '
You can specify a list of users to allow duplicate awards on the same photo for. ';
buf += "Separate each item with ';'. Don't include spaces before or after items.
";
buf += '
Update Check
';
buf += '
This option will notify you if a new version of flickr Award Counter is released. ';
buf += ' It will check with phazeshift.co.uk once a week for a new version. The current status is displayed at the top of this window when it is enabled.
';
buf += '
Favourites
';
buf += '
This option will show the number of users that have added a picture as a favourite. This option is a little slower, as it requires a second call to flickr.
';
buf += '
Galleries
';
buf += '
This option will show the number of galleries a picture has been added to. This option is a little slower, as it requires a second call to flickr.
';
buf += '
Hide zero count
This option will hide awards with a count of zero.
This option will allow you to save and restore your awards from the web. To save your settings, leave the id box blank and click save. ';
buf += 'Make a note of your settings ID when the save is completed. To load your settings, enter your settings ID and click load. these settings are ';
buf += 'not private or encrypted and can be accessed from any machine with your ID.