Skip to content
Permalink
Browse files
fix(update-by-self): use onSubmit
  • Loading branch information
h13i32maru committed Jun 26, 2020
1 parent fc9abfb commit 211bf7fd5fc36e58ccfa2c9d3f700b7a52484e5a
Showing 1 changed file with 7 additions and 96 deletions.
@@ -1,97 +1,8 @@
interface Window {
__UPDATE_BY_SELF__: any;
{
window.addEventListener('submit', () => {
// サブミットされてすぐには更新されないので、1s待つ
setTimeout(() => {
console.log('UPDATE_BY_SELF:');
}, 1000);
}, true);
}

(function(){
window.__UPDATE_BY_SELF__ = window.__UPDATE_BY_SELF__ || {};

const loginName = '_loginName_';
let lastTime = {
addComment: 0,
updateComment: 0
};

function checkAddComment(mutations) {
for (const mutation of mutations) {
for (const el of Array.from(mutation.addedNodes) as HTMLElement[]) {
if (!el || !el.querySelector) continue;

const authorEl = el.querySelector('.author');
if (!authorEl) continue;

const author = authorEl.textContent;
if (author !== loginName) continue;

const now = Date.now();
if (lastTime.addComment + 300 > now) continue;

lastTime.addComment = now;
console.log('UPDATE_BY_SELF:');

return;
}
}
}

function checkUpdateComment(mutations) {
const el = mutations[0].target;
if (!el || !el.classList) return;

if (!el.classList.contains('comment-body')) return;

const now = Date.now();
if (lastTime.updateComment + 300 > now) return;

lastTime.updateComment = now;
console.log('UPDATE_COMMENT_BY_SELF:');
}

// conversation tab
{
const target = document.querySelector('.js-discussion');
if (target) {
if (window.__UPDATE_BY_SELF__.conversation) {
window.__UPDATE_BY_SELF__.conversation.disconnect();
}

const observer = new MutationObserver((mutations) => {
checkAddComment(mutations);
checkUpdateComment(mutations);
});

observer.observe(target, {subtree: true, childList: true});

window.__UPDATE_BY_SELF__.conversation = observer;
}
}

// files tab
{
const target = document.querySelector('#files.diff-view');
if (target) {
if (window.__UPDATE_BY_SELF__.files) {
window.__UPDATE_BY_SELF__.files.disconnect();
}

const observer = new MutationObserver((mutations) => {
checkAddComment(mutations);
checkUpdateComment(mutations);
});

observer.observe(target, {subtree: true, childList: true});

window.__UPDATE_BY_SELF__.files = observer;
}
}

// submit review
{
window.addEventListener('beforeunload', () =>{
const submitButton = document.querySelector('#submit-review button[type="submit"]');
// if it has disabled, submit review
if (submitButton && submitButton.hasAttribute('disabled')) {
console.log('UPDATE_BY_SELF:');
}
});
}
})();

0 comments on commit 211bf7f

Please sign in to comment.