Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dupe IP helper
// ==UserScript==
// @name Dupe review helper
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Watches out for reviews made by the same IP subnet
// @author anevins12
// @match https://wordpress.org/support/topic/*
// @grant none
// ==/UserScript==
(function(isPluginReview, checkData) {
'use strict';
var $ = jQuery,
pluginName,
reviewCollection = {
plugin: '',
ip: 0,
url: ''
},
namespace,
matchedStoredCollection,
identifierClass = 'wp-duplicate-review-flag',
message,
messageClass = 'wp-duplicate-message',
removeData = $('<button class="wp-remove-data">🚩 Clear duplicated reviews</button>');
if (isPluginReview($)) {
reviewCollection.plugin = $('.bbp-breadcrumb-forum').text();
reviewCollection.ip = $('.bbp-topic-ip').text().replace(/["'()]/g, '');
reviewCollection.url = window.location.pathname;
namespace = 'wp-reviewee-';
matchedStoredCollection = JSON.parse(localStorage.getItem(namespace + reviewCollection.ip));
removeData
.css({
'fontSize': '80%',
'position': 'relative',
'top': '-1em'
})
.on('click', function() {
var button = $(this),
message = $('.' + identifierClass);
checkData(namespace, true);
button
.css('border-color', 'lawngreen')
.html('🚩 Duplicated reviews cleared')
.delay(2000)
.fadeOut();
if (message.length !== 0) {
message.remove();
}
});
// If reviews have been stored
if (checkData(namespace)) {
$('.bbp-breadcrumb').prepend(removeData);
}
if (matchedStoredCollection) {
message = $('<a class="' + messageClass + ' author-badge author-badge-plugin' + identifierClass + '" href="' + matchedStoredCollection.url + '">Duplicate review</a>');
// If this is a new review but for the same plugin & IP
if (matchedStoredCollection.plugin === reviewCollection.plugin &&
matchedStoredCollection.ip === reviewCollection.ip &&
matchedStoredCollection.url !== reviewCollection.url) {
message.css({
'background': 'red',
'color': 'white',
'fontWeight': '700',
'fontSize': '150%',
'margin-top': '-5px',
'margin-left': '-1em',
'textDecoration': 'underline'
});
$('.bbp-topic-ip').append(message);
}
} else {
localStorage.setItem(namespace + reviewCollection.ip, JSON.stringify(reviewCollection));
}
}
})(isPluginReview, checkData);
function isPluginReview($) {
'use strict';
var ratings = $('.single-topic .wporg-ratings');
if (ratings.length !== 0) {
return true;
}
return false;
}
function checkData(name, destroy) {
/*
* Returns truthy if is data stored
* Destroys data if argument passed
*/
'use strict';
var reviewData = [],
check = function(key) {
if (/^/ + name) {
reviewData.push(key);
if (destroy) {
localStorage.removeItem(key);
}
}
};
Object
.keys(localStorage)
.forEach(function(key) {
check(key);
});
if (reviewData.length > 1) {
return true;
} else {
return false;
}
}
@anevins12
Copy link
Author

anevins12 commented May 18, 2018

Feedback:

  • Replace the "Dupe" text, instead make it a bit more noticable, maybe inject it like the flag yo usee for authors, adding that to it with "Duplicate review" as the label and some eye-catching color might be nice, makes it faster to spot when scanning
  • I'm also wondering about the lifetime of the data store, it seems like it has no limit to the lfietime, that's going to quickly become a bit of a memory hog with time consideringthe amount of reviews we receive
    perhaps introducing some kind of indicator of how much data is stored with the ability to purge it easily ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment