From 3d158455ff2dbfde51788da5a6043242890a0821 Mon Sep 17 00:00:00 2001 From: Filip Jurcak Date: Thu, 27 Jul 2017 11:40:37 +0200 Subject: [PATCH 1/2] showUsername final --- src/content.js | 8 +- src/libs/transformations/showUsername.js | 108 +++++++++++------------ 2 files changed, 56 insertions(+), 60 deletions(-) diff --git a/src/content.js b/src/content.js index 8e21488..1753512 100644 --- a/src/content.js +++ b/src/content.js @@ -14,8 +14,7 @@ import alignLabels from "./libs/transformations/alignLabels"; import appendTo from "./libs/transformations/appendTo"; import showUsername from "./libs/transformations/showUsername"; import assignMe from "./libs/transformations/assignMe"; -import bindLabelsKeyboardShortcuts - from "./libs/transformations/bindLabelsKeyboardShortcuts"; +import bindLabelsKeyboardShortcuts from "./libs/transformations/bindLabelsKeyboardShortcuts"; const s = document.createElement("script"); s.src = chrome.runtime.getURL("agent.js"); // eslint-disable-line no-undef @@ -28,7 +27,7 @@ storage.load().then(() => { case ROUTES.ISSUE: // Enable when it will work properly // assignMe(); - // showUsername(route); + showUsername(route); expandAll(); rotateDiscussion("notes-list"); expandSidePanel(); @@ -40,8 +39,7 @@ storage.load().then(() => { case ROUTES.ISSUES: filterItems("filtered-search-box"); alignLabels(route); - // Enable when it will work properly - // showUsername(route); + showUsername(route); break; } diff --git a/src/libs/transformations/showUsername.js b/src/libs/transformations/showUsername.js index e11a640..f58c1c4 100644 --- a/src/libs/transformations/showUsername.js +++ b/src/libs/transformations/showUsername.js @@ -6,69 +6,69 @@ import getUsername from "../helpers/getUsername"; export default route => { switch (route) { case ROUTES.MR: - case ROUTES.ISSUE: + case ROUTES.ISSUE: { + const commitsTab = document.getElementsByClassName("commits-tab")[0]; + commitsTab.children[0].click(); + const notesTab = document.getElementsByClassName("notes-tab")[0]; + notesTab.children[0].click(); // eslint-disable-next-line no-plusplus for (let j = 0; j < 7; j++) { const className = [ "author has-tooltip", "author_link bold", "author_link has-tooltip", - "note-header-info", + "note-header-author-name", "author_link", - "discussion-header", + "inline discussion-headline-light", "commit-author-link has-tooltip", + "avatar has-tooltip s36 hidden-xs", ]; - const a = document.getElementsByClassName(className[j]); - if (a !== undefined) { - // eslint-disable-next-line no-plusplus - for (let i = 0; i < a.length; i++) { - let username; - if (j === 2 || j === 4 || j === 6) username = getUsername(a[i]); - switch (j) { - case 0: - // Author - a[i].innerText = a[i].getAttribute("title").slice(1); - continue; - case 1: - // Assignees - a[i].children[1].innerText = a[i].children[2].innerText.slice( - 1 - ); - continue; - case 2: - // Participants - a[i].setAttribute("title", username); - continue; - case 3: - // Discussion - if (a[i].children[0].childElementCount === 2) { - a[i].children[0].children[0].innerText = a[ - i - ].children[0].children[1].innerText.slice(1); - a[i].children[0].removeChild(a[i].children[0].children[1]); - } - continue; - case 4: - a[i].childNodes[0].innerText = username; - continue; - case 5: - a[i].children[2].innerText = a[i].children[2].innerText.replace( - `@${getUsername(a[i].children[1])}`, - "" - ); - continue; - case 6: - // Commits - // eslint-disable-next-line no-shadow - setTimeout((a, i) => { - // eslint-disable-next-line no-param-reassign - a[i].innerText = username; - }, 1000); + let a = document.getElementsByClassName(className[j]); + if (j === 6 || j === 7) a = [].slice.call(a); + // eslint-disable-next-line no-plusplus + for (let i = 0; i < a.length; i++) { + let username; + if (j === 2 || j === 4 || j === 6) username = getUsername(a[i]); + switch (j) { + case 0: + // Author + a[i].innerText = a[i].getAttribute("title").slice(1); + continue; + case 1: + // Assignees + a[i].children[1].innerText = a[i].children[2].innerText.slice(1); + continue; + case 2: + // Participants + a[i].setAttribute("title", username); + continue; + case 3: + // Discussion + a[i].innerText = getUsername(a[i].parentElement); + a[i].parentElement.children[1].remove(); + continue; + case 4: + a[i].children[0].innerText = username; + continue; + case 5: + a[i].innerText = a[i].innerText.replace( + `@${getUsername(a[i].parentElement.children[1])}`, + "" + ); + continue; + case 6: + a[i].innerText = username; + continue; + case 7: { + const userName = getUsername(a[i].parentElement); + a[i].setAttribute("alt", `${userName}'s avatar`); + a[i].setAttribute("title", `${userName}`); } } } } break; + } case ROUTES.MRS: case ROUTES.ISSUES: { let a = document.getElementsByClassName("author_link"); @@ -76,12 +76,10 @@ export default route => { // eslint-disable-next-line array-callback-return a.map(element => { const username = getUsername(element); - if (element.classList.contains("has-tooltip")) { - element.setAttribute("title", `Assigned to ${username}`); - } else { - // eslint-disable-next-line no-param-reassign - element.children[0].innerText = username; - } + // eslint-disable-next-line no-unused-expressions + element.classList.contains("has-tooltip") + ? element.setAttribute("title", `Assigned to ${username}`) + : (element.children[0].innerText = username); // eslint-disable-line no-param-reassign }); break; } From f06204459b6904085824fc8858700a905d6eac58 Mon Sep 17 00:00:00 2001 From: Strajk Date: Thu, 27 Jul 2017 12:21:40 +0200 Subject: [PATCH 2/2] Refactor multiple occurrences to common block --- src/content.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/content.js b/src/content.js index 37a9a88..004cca0 100644 --- a/src/content.js +++ b/src/content.js @@ -14,8 +14,10 @@ import alignLabels from "./libs/transformations/alignLabels"; import appendTo from "./libs/transformations/appendTo"; import showUsername from "./libs/transformations/showUsername"; import assignMe from "./libs/transformations/assignMe"; -import bindLabelsKeyboardShortcuts from "./libs/transformations/bindLabelsKeyboardShortcuts"; -import discussionOverComment from "./libs/transformations/discussionOverComment"; +import bindLabelsKeyboardShortcuts + from "./libs/transformations/bindLabelsKeyboardShortcuts"; +import discussionOverComment + from "./libs/transformations/discussionOverComment"; const s = document.createElement("script"); s.src = chrome.runtime.getURL("agent.js"); // eslint-disable-line no-undef @@ -23,12 +25,18 @@ s.src = chrome.runtime.getURL("agent.js"); // eslint-disable-line no-undef storage.load().then(() => { const route = pathnameToRoute(location.pathname); + + // Common + // --- + showUsername(route); + + // Route specific + // --- switch (route) { // eslint-disable-line default-case case ROUTES.MR: case ROUTES.ISSUE: // Enable when it will work properly // assignMe(); - showUsername(route); expandAll(); rotateDiscussion("notes-list"); expandSidePanel(); @@ -41,7 +49,6 @@ storage.load().then(() => { case ROUTES.ISSUES: filterItems("filtered-search-box"); alignLabels(route); - showUsername(route); break; }