Skip to content

Instantly share code, notes, and snippets.

@makyen
Created March 10, 2022 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makyen/f7566383b8a0995ee0294cbd5ef0a28a to your computer and use it in GitHub Desktop.
Save makyen/f7566383b8a0995ee0294cbd5ef0a28a to your computer and use it in GitHub Desktop.
Patch StackExchange.realtime.reloadPosts to work with the new data format returned from /ajax-load-realtime/.
// ==UserScript==
// @name SE: Quick fix SE.realtime.reloadPosts
// @namespace MakyenStackExchangeAdjustments
// @description Patch StackExchange.realtime.reloadPosts to work with the new data format returned from /ajax-load-realtime/.
//
// @match *://*.stackoverflow.com/*
// @match *://*.superuser.com/*
// @match *://*.serverfault.com/*
// @match *://*.askubuntu.com/*
// @match *://*.stackapps.com/*
// @match *://*.stackexchange.com/*
// @match *://*.mathoverflow.net/*
//
// @exclude *://api.*
// @exclude *://blog.*
// @exclude *://chat.*
// @exclude *://data.*
// @exclude *://stackoverflow.com/advertising*
// @exclude *://stackoverflow.com/talent*
// @exclude *://stackoverflow.com/teams*
//
// @require https://cdn.jsdelivr.net/gh/makyen/extension-and-userscript-utilities@94cbac04cb446d35dd025974a7575b25b9e134ca/executeInPage.js
//
// @version 1.0.0
// ==/UserScript==
/* globals StackExchange, $, makyenUtilities */
(function() {
'use strict';
function inPagePatchSEReloadPosts() {
StackExchange.ready(() => {
//2022-03-09 Patch an issue with realtime reloading posts.
//The code in reloadPosts uses only the .then() from $.ajax, so we don't need to fully
// support all possible options when temporarily replacing $.ajax.
const origSEReloadPosts = StackExchange.realtime.reloadPosts;
StackExchange.realtime.reloadPosts = function() {
const origAjax = $.ajax;
$.ajax = function() {
return origAjax.apply(this, arguments).then((response) => {
try {
return JSON.parse(response).Html;
} catch {
return response;
}
});
}
const toReturn = origSEReloadPosts.apply(this, arguments);
//We only want this change to $.ajax to be effective for this call to reloadPosts, so put it back.
$.ajax = origAjax;
return toReturn;
};
});
}
makyenUtilities.executeInPage(inPagePatchSEReloadPosts);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment