Last active
January 24, 2022 14:33
-
-
Save anevins12/02728f7c256adae7baafc5ddb1b15982 to your computer and use it in GitHub Desktop.
Dupe IP helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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
Feedback:
perhaps introducing some kind of indicator of how much data is stored with the ability to purge it easily ?