Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Execute this code on https://steamcommunity.com/my/commenthistory
- The newest versions are available on BitBucket: https://bitbucket.org/SoftJustman/steamcommentscleaner
- Created by Justman (steamcommunity.com/id/justman666)
- Specially for Miped.ru
- Special thx for script testers from Reddit :)
- */
- /*
- Changable constants
- */
- var FIRST_PAGE_TO_CLEAR = 1; // Number of first page to be cleared
- var LAST_PAGE_TO_CLEAR = -1; // Number of last page to clear. -1 if you need to delete all of them
- // Example (2, 2) will clear 2nd page only
- var PRINT_EXECUTED_COMMANDS = false; // Print everything script does (It's a lot)
- /*
- Constants
- */
- var COMMENTS_PAGE = "https://steamcommunity.com/my/commenthistory"; // URL to page with comments
- var FORUMS_JS_URL = [
- "https://community.cloudflare.steamstatic.com/public/javascript/global.js",
- "https://steamcommunity-a.akamaihd.net/public/javascript/forums.js",
- "https://community.edgecast.steamstatic.com/public/javascript/forums.js"
- ]; // URL to page with forums.js
- var FORUM_JS_LOAD_TIMEOUT = 10000; // ms
- var USER_STEAM_PROFILE_URL = jQuery("#global_actions > a").attr("href").replace(/\/$/, ""); // https://steamcommunity.com/id/$ID
- var PAGE_REQUESTS_INTERVAL = 1000; // ms
- var TIMESTAMP_DIFF = 3; // Maximum difference between timestamps
- var REGEXP =
- {
- "body": /<body.{1,}?>([.\s\S]{1,})<\/body>/, // Body innerHTML (No depending on attributes)
- "link": /^(.{1,}?)(?:\?(.{1,}?))?$/, // Link -> url + data
- "initFTopic": /InitializeForumTopic\s?\([^"]+?(?:"[^"]+?"[^"]+?)+?[^"]+?\);/, // Protected from ); in quotes
- "initCThread": /InitializeCommentThread\s?\([^"]+?(?:"[^"]+?"[^"]+?)+?[^"]+?\);/ // Protected from ); in quotes
- };
- /*
- Utility
- */
- var pages_amount = jQuery(".pageLinks .pagelink:last").html(); // Amount of pages
- if (!pages_amount) {
- pages_amount = 1;
- } else {
- pages_amount = +(pages_amount);
- }
- var pages = []; // Array with page bodies
- var comments = []; // Array with comments to clean
- var execute = function(txt) // Replacer to eval to be able to see commands printed
- {
- if (PRINT_EXECUTED_COMMANDS) {
- console.log(txt);
- }
- eval(txt);
- }
- /*
- Functions
- */
- var loadPagesFrom = function(fromPageNumber, callback) // Loading page body with comments and adding it to pages as div
- {
- if (fromPageNumber > pages_amount || (LAST_PAGE_TO_CLEAR != -1 && fromPageNumber > LAST_PAGE_TO_CLEAR)) {
- console.log(" Loaded pages");
- console.log(pages);
- if (typeof callback == "function") {
- callback();
- }
- return;
- }
- console.log(" Loading page #" + fromPageNumber);
- if (location.search.match(new RegExp("p=" + fromPageNumber)) || fromPageNumber == 1) {
- var elem = document.createElement("div");
- elem.innerHTML = document.body.innerHTML;
- pages.push(elem);
- console.log(" Loaded page #" + fromPageNumber);
- loadPagesFrom(fromPageNumber + 1, callback);
- return;
- }
- var search = location.search.replace(/\?/, ""); // Delete first ? if it exists
- if (search) {
- // Search is not empty
- if (search.match(/p=[0-9]{1,}/)) {
- // Page number is already stated (it must be 1)
- search = search.replace(/p=[0-9]{1,}/, "p=" + fromPageNumber)
- } else {
- // No page number but have some search parameters
- search += "&p=" + fromPageNumber;
- }
- } else {
- // empty search
- search = "p=" + fromPageNumber;
- }
- jQuery.ajax({
- "success": function(data, status)
- {
- var inner = data.match(REGEXP.body)[1];
- var elem = document.createElement("div");
- elem.innerHTML = inner;
- pages.push(elem);
- console.log(" Loaded page #" + fromPageNumber);
- loadPagesFrom(fromPageNumber + 1, callback);
- },
- "fail" : function()
- {
- console.error(" !!!Unable to load page");
- },
- "data": search,
- "method": "GET",
- "url": COMMENTS_PAGE
- });
- };
- var loadURLsFrom = function(fromPageNumber, callback) // Loading URLs and adding them to comments as objects
- {
- if (fromPageNumber > pages.length) {
- console.log(" Loaded URLs");
- console.log(comments);
- if (typeof callback == "function") {
- callback();
- }
- return;
- }
- jQuery(pages[fromPageNumber - 1]).find(".commenthistory_comment .comment_item_title a").each(function(index, a)
- {
- var info = {}; // Info about comment
- var link = a.href;
- link = link.replace(/#/,"/"); // Getting rid of # (replacable with /)
- if (link.match(/\/discussions\//)) {
- info["isForum"] = true;
- } else {
- info["isForum"] = false;
- }
- var m = link.match(REGEXP.link);
- info["link"] = link; // Full link
- info["url"] = m[1]; // Link with no GET arguments
- info["data"] = m[2]; // GET arguments (key=val&key=val...)
- if (info["data"] && info["data"].match(/tscn=[0-9]{1,}/)) {
- info["timestamp"] = info["data"].match(/tscn=([0-9]{1,})/)[1]; // Timestamp
- }
- if (info["isForum"]) {
- if (info["data"] && info["data"].match(/tscn=/)) {
- info["isForumTopic"] = false;
- } else {
- info["isForumTopic"] = true;
- }
- }
- info["text"] = jQuery(a).closest(".commenthistory_comment").find(".comment_text").text().replace(/\s/g, "");
- comments.push(info);
- });
- console.log(" Loaded URLs from page #" + (fromPageNumber + FIRST_PAGE_TO_CLEAR - 1));
- loadURLsFrom(fromPageNumber + 1, callback);
- }
- var clearURLfromIndex = function(index, callback) // Clearing comment form comments array by its index
- {
- if (index >= comments.length) {
- console.log(" Cleared URLs");
- console.log(comments);
- if (typeof callback == "function") {
- window.location.hash = ""; // Removing previously set hash
- callback();
- }
- return;
- }
- window.location.hash = "#p0"; // Can't initialize Comment thread without this hash (Crutch ._ .)
- jQuery.ajax({
- "success": function(data, status)
- {
- try {
- comments[index]["html"] = document.createElement("div");
- comments[index]["html"].innerHTML = data;
- if (jQuery(comments[index]["html"]).find("#message").length) {
- throw new Error(jQuery(comments[index]["html"]).find("#message").text());
- }
- if (comments[index]["isForum"]) {
- var initFTopic = jQuery(comments[index]["html"]).find("script:contains(InitializeForumTopic)").html(); // Code to be executed
- initFTopic = initFTopic.match(REGEXP.initFTopic)[0]; // Getting main command
- execute(initFTopic);
- var initCThread = jQuery(comments[index]["html"]).find("script:contains(InitializeCommentThread)").html(); // Code to be executed
- initCThread = initCThread.match(REGEXP.initCThread)[0]; // Getting main command
- initCThread = initCThread.replace(/\{/, "{\"no_paging\": true, "); // Adding no paging to prevent error
- execute(initCThread);
- if (comments[index]["isForumTopic"]) {
- // Topic
- var deleteTopic = jQuery(comments[index]["html"]).find(".forum_op a[href*=\"DeleteTopic\"]");
- var argument = jQuery(deleteTopic).attr("href").match(/\(([.\s\S]{1,}?)\)/)[1].replace(/[\s'"]/g, "");
- new Ajax.Request(
- g_rgForumTopics[argument].GetActionURL("deletetopic"), {
- "method": "POST",
- "parameters": g_rgForumTopics[argument].ParametersWithDefaults()
- });
- } else {
- // Message
- var messagesByUser = jQuery(comments[index]["html"]).find(".commentthread_comment .commentthread_comment_avatar a[href*=\"" + USER_STEAM_PROFILE_URL + "\"]").closest(".commentthread_comment"); // Messages on the page by user
- var ifFoundMessage = false;
- messagesByUser.each(function(i, message)
- {
- var text = jQuery(message).find(".commentthread_comment_text").text().replace(/\s/g, ""); // innerText of message
- var timestamp = jQuery(message).find(".commentthread_comment_timestamp").attr("data-timestamp"); // Timestamp of message
- if (text == comments[index]["text"] && Math.abs(+(timestamp) - +(comments[index]["timestamp"])) < TIMESTAMP_DIFF) {
- var deleteComment = jQuery(message).find("a[href*=\"DeleteComment\"]").attr("href");
- if (!deleteComment) {
- throw new Error("\nWas unable to find \"Delete\" button. Possibly you can't delete message from this group\n");
- }
- deleteComment = deleteComment.replace(/javascript:/, "");
- var arguments = deleteComment.match(/\(([.\s\S]{1,}?),([.\s\S]{1,}?)\)/); // Arguments
- // arguments looks like: " 'STRING' ". It may have [\s'"] in it
- execute("g_rgCommentThreads[" + arguments[1] + "].GetForumTopic().m_rgForumData.permissions.can_moderate = 1;"); // Need to prevent confirmation (execute is used just to be able to replace it with console.log. Also this is effective as we keep argument as it was at DeleteComment())
- execute("g_rgCommentThreads[" + arguments[1] + "].DeleteComment(" + arguments[2] + ");");
- ifFoundComment = true;
- return false; // break
- }
- });
- }
- } else {
- // Comment
- if (comments[index]["url"].match(/\/id\//) && !jQuery(comments[index]["html"]).find(".commentthread_comments").length) {
- throw new Error("\nCan not delete comment. As profile owner locked his page and deleted you from friends\n");
- }
- var initCThread = jQuery(comments[index]["html"]).find("script:contains(InitializeCommentThread)").html(); // Code to be executed
- initCThread = initCThread.match(REGEXP.initCThread)[0]; // Getting main command
- initCThread = initCThread.replace(/\{/, "{\"no_paging\": true, "); // Adding no paging to prevent error
- execute(initCThread);
- var commentsByUser = jQuery(comments[index]["html"]).find(".commentthread_comment .commentthread_comment_avatar a[href*=\"" + USER_STEAM_PROFILE_URL + "\"]").closest(".commentthread_comment"); // Comments on the page by user
- var ifFoundComment = false;
- commentsByUser.each(function(i, comment)
- {
- var text = jQuery(comment).find(".commentthread_comment_text").text().replace(/\s/g, ""); // innerText of comment
- var timestamp = jQuery(comment).find(".commentthread_comment_timestamp").attr("data-timestamp"); // Timestamp of comment
- if (text == comments[index]["text"] && Math.abs(+(timestamp) - +(comments[index]["timestamp"])) < TIMESTAMP_DIFF) {
- var deleteComment = jQuery(comment).find("a[href*=\"DeleteComment\"]").attr("href").replace(/javascript:/, "");
- if (!deleteComment) {
- throw new Error("\nWas unable to find \"Delete\" button\n");
- }
- execute(deleteComment);
- ifFoundComment = true;
- return false; // break
- }
- });
- if (!ifFoundComment) {
- throw new Error("\nWas unable to find users comment\n");
- }
- }
- console.log(" Cleaned comment #" + (index + 1));
- } catch (e) {
- console.error(" !!! Can not clean comment #" + (index + 1));
- console.error(comments[index]);
- console.error(e);
- }
- var nextTimeout = setTimeout(function() { clearURLfromIndex(index + 1, callback); }, PAGE_REQUESTS_INTERVAL);
- },
- "fail" : function()
- {
- console.error(" !!!Unable to send request");
- },
- "data": comments[index]["data"],
- "method": "GET",
- "url": comments[index]["url"]
- });
- };
- var loadForumJS = function(i, callback, onFail)
- {
- var timeout = setTimeout(function () { // Setting timeout as reject of load is not supported with getScript
- console.error(" Error occured loading forum.js #" + i + " - timeout");
- if (i + 1 < FORUMS_JS_URL.length) {
- // Still have some links
- console.error(" Trying next link");
- loadForumJS(i + 1, callback, onFail); // Trying another one
- } else {
- // Tryed everything
- console.error(" Mo links left");
- if (typeof onFail == "function") {
- onFail();
- }
- }
- }, FORUM_JS_LOAD_TIMEOUT);
- console.log(" Trying forum.js #" + i + ". Will try next in " + (FORUM_JS_LOAD_TIMEOUT / 1000) + " seconds if this wont work");
- jQuery.getScript(FORUMS_JS_URL[i]).done(function() { // Require forum.js to delete forum messages
- // Needed script is loaded and no errors detected
- console.log(" Loaded forum.js #" + i);
- clearTimeout(timeout); // Stop timeout
- if (typeof callback == "function") {
- callback();
- }
- }).fail(function() {
- console.error(" Error occurred loading forum.js #" + i);
- console.error(" Trying next link");
- if (i + 1 < FORUMS_JS_URL.length) {
- loadForumJS(i + 1, callback, onFail); // Trying another one
- } else {
- if (typeof onFail == "function") {
- onFail();
- }
- }
- });
- };
- /*
- Main
- */
- loadForumJS(0, function()
- {
- loadPagesFrom(FIRST_PAGE_TO_CLEAR, function()
- {
- loadURLsFrom(1, function()
- {
- clearURLfromIndex(0, function()
- {
- alert("Done");
- });
- });
- });
- }, function()
- {
- alert("Can not load forum.js!");
- });
Add Comment
Please, Sign In to add comment