0

I have a simple Greasemonkey script here:

// Replace lines breaks in table cells
// ######################################

function newlinesToBreaks (string) {
  return string.split('\n').map(function (str) {
    return str.trim()
  }).join('<br>')
}

var cells = document.querySelectorAll('td.listBodyCell');

for (var i = 0, il = cells.length; i < il; i++) {
  cell = cells[i]
  cell.innerHTML = newlinesToBreaks(cell.innerHTML)
}

It replaces line breaks with <br> tags so that the formatting on the web page is displayed correctly.

It works fine when the whole page loads.

The web page displays a list of updates made to a call. When there are more than 20 updates, there is a "next" icon, and more updates can be loaded, via XMLHttpRequest.

The response delivered by that call is in JSON format.

And the output of that does not trigger the GM script, presumably because the entire page is not refreshed.

Is there any way around this or is it a limitation of loading content via XMLHttpRequest?

4532066
  • 2,042
  • 5
  • 21
  • 48
  • Have done similar by writing an interceptor method that allows running your own code when request succeeds. Was fairly easy in my case because page uses jQuery `$.ajax` but can be done overloading native XMLHttpRequest also. Can also use MutationObserver api. Is there a library method used to make the request? – charlietfl Feb 14 '17 at 12:57
  • Thanks for your response. I'm not sure how to check if a library method is used to make the request. Sorry... – 4532066 Feb 14 '17 at 13:38
  • what scripts are loaded in the page? – charlietfl Feb 14 '17 at 13:40
  • There are a lot of different scripts loaded in the page, and I can see 3 JS files. None are e.g. a standard jQuery library, the JS is a big block of text as all formatting has been stripped out. I can find mention of jQuery once in one of the JS files. I think I'm probably out of my depth here - but thanks for taking the time to reply. – 4532066 Feb 14 '17 at 14:13
  • Have you checked out the [waitForKeyElements()](http://stackoverflow.com/questions/8281441/fire-greasemonkey-script-on-ajax-request/8283815#8283815) function? Might be a good fit. – Hellion Feb 15 '17 at 18:13
  • @Hellion thanks for your suggestion. That certainly looks hopeful. I had a look at the link posted from your link (http://stackoverflow.com/questions/11195658/run-greasemonkey-script-on-the-same-page-multiple-times/11197969#11197969), but I can't see how I would modify my code to include the `waitForKeyElements` function. Would I have to create a new function similar to the `addCustomSearchResult` one used in the example code? Thanks again – 4532066 Feb 15 '17 at 21:49
  • Yes, you would need a new function, but it would probably be very simple; something like `function doMyThing(jnode) {jnode.innerHTML = newlinesToBreaks(jnode.innerHTML)};` and then your main code would be `waitForKeyElements('td.ListBodyCell', doMyThing)` – Hellion Feb 15 '17 at 22:19

0 Answers0