Created
March 10, 2022 17:44
-
-
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/.
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 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