From e702082bf91024cd2c1a225b8020bebcf9ef19cc Mon Sep 17 00:00:00 2001 From: robario Date: Wed, 28 Aug 2013 14:18:05 +0900 Subject: [PATCH] Fix #1 to work around the pjax replacing --- github-diff-ws.user.js | 47 +++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/github-diff-ws.user.js b/github-diff-ws.user.js index ad7ce53..91d1f5b 100644 --- a/github-diff-ws.user.js +++ b/github-diff-ws.user.js @@ -7,23 +7,32 @@ return; } - var a = document.createElement('a'); - a.setAttribute('class', 'minibutton'); - if (/[&?]w=/.test(location.search)) { - a.innerText = 'Cognize WS'; - a.onclick = function() { - var search = location.search.replace(/[&?]w=/g, ''); - if (search) { - location.search = search; - } else { - location.href = location.pathname + location.hash; - } - }; - } else { - a.innerText = 'Ignore WS'; - a.onclick = function() { - location.search += location.search ? '&w=' : '?w='; - }; - } - toc.getElementsByClassName('explain')[0].insertBefore(a, toc.getElementsByClassName('show-diff-stats')[0]); + var updateUI = function(toc) { + var a = document.createElement('a'); + a.setAttribute('class', 'minibutton'); + if (/[&?]w=/.test(location.search)) { + a.innerText = 'Cognize WS'; + a.onclick = function() { + var search = location.search.replace(/[&?]w=/g, ''); + if (search) { + location.search = search; + } else { + location.href = location.pathname + location.hash; + } + }; + } else { + a.innerText = 'Ignore WS'; + a.onclick = function() { + location.search += location.search ? '&w=' : '?w='; + }; + } + toc.getElementsByClassName('explain')[0].insertBefore(a, toc.getElementsByClassName('show-diff-stats')[0]); + }; + + document.getElementById('js-repo-pjax-container').addEventListener('DOMNodeInserted', function(event) { + if (event.target.id == 'toc') { + updateUI(event.target); + } + }, false); + updateUI(toc); })();