Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mmarj/8e01a3e6dd9cfe5ea246711d0c6e985b to your computer and use it in GitHub Desktop.
Save mmarj/8e01a3e6dd9cfe5ea246711d0c6e985b to your computer and use it in GitHub Desktop.
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment