Skip to content

Commit

Permalink
fix(update-by-self): use onSubmit
Browse files Browse the repository at this point in the history
  • Loading branch information
h13i32maru committed Jun 26, 2020
1 parent fc9abfb commit 211bf7f
Showing 1 changed file with 7 additions and 96 deletions.
103 changes: 7 additions & 96 deletions src/Electron/Component/WebViewComponentInjection/update-by-self.ts
@@ -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.