From 5c069e1a35d71b44cae927ef2ea3dc022bded1b2 Mon Sep 17 00:00:00 2001 From: tomcat0090 Date: Tue, 17 Jan 2023 19:44:32 +0900 Subject: [PATCH 1/2] Fix failure to get project name with segments Projects with segments cannot get the project name. As a side note, if the project name is not registered in Toggl and the segment name matches the project name in Toggl, it will be recognized as the project name. This commit is for the list only, and the task detail screen will be fixed separately. --- src/content/todoist.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/content/todoist.js b/src/content/todoist.js index 040e6d810..72daeda97 100644 --- a/src/content/todoist.js +++ b/src/content/todoist.js @@ -88,6 +88,11 @@ togglbutton.render( const rootEl = elem.closest('.task_list_item'); const content = rootEl.querySelector('.task_list_item__content'); + const projects = []; + for (const [key, value] of Object.entries(togglbutton.projects)) { + projects.push(value.name); + } + const descriptionSelector = () => { const text = content.querySelector('.task_content'); return text ? text.textContent.trim() : ''; @@ -98,6 +103,10 @@ togglbutton.render( if (document.querySelector('.project_view h1 span.simple_content')) { project = document.querySelector('.project_view h1 span.simple_content').textContent.trim(); + const section = $('.section_head__title .simple_content', elem.closest('section:not(.section__default)')).textContent.trim(); + if (! isExist(project, projects) && isExist(section, projects)) { + project = section; + } } else if (document.getElementById(`item_${projectId}`)) { // (legacy?) project ID element const projectContent = document.getElementById(`item_${projectId}`).querySelector('.content'); @@ -105,6 +114,7 @@ togglbutton.render( } else if (rootEl.querySelector('.task_list_item__project')) { // Project name shown alongside the task in UI project = rootEl.querySelector('.task_list_item__project').textContent.trim(); + project = separateAndCheck(project, projects); } else if (document.querySelector('[data-project-id] .simple_content')) { project = document.querySelector('[data-project-id] .simple_content').textContent; } else { @@ -255,3 +265,22 @@ function getParentIfProject (elem) { return project; } + +function separateAndCheck (str, projects) { + const words = str.split('/'); + if (words.length < 2) { + return str; + } + const project = words[0].trim(); + const section = words[1].trim(); + if (isExist(project, projects)) { + return project; + } else if (isExist(section, projects)) { + return section; + } + return str; +} + +function isExist(str, arr) { + return arr.some((p) => p === str); +} From 704c4a673ad41bced584bce33d439b2440e9bb20 Mon Sep 17 00:00:00 2001 From: tomcat0090 Date: Fri, 20 Jan 2023 10:51:47 +0900 Subject: [PATCH 2/2] Change project existence functions to native Thanks for the review @glensc. --- src/content/todoist.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/content/todoist.js b/src/content/todoist.js index 72daeda97..47173f26f 100644 --- a/src/content/todoist.js +++ b/src/content/todoist.js @@ -104,7 +104,7 @@ togglbutton.render( if (document.querySelector('.project_view h1 span.simple_content')) { project = document.querySelector('.project_view h1 span.simple_content').textContent.trim(); const section = $('.section_head__title .simple_content', elem.closest('section:not(.section__default)')).textContent.trim(); - if (! isExist(project, projects) && isExist(section, projects)) { + if (! projects.includes(project) && projects.includes(section)) { project = section; } } else if (document.getElementById(`item_${projectId}`)) { @@ -273,14 +273,10 @@ function separateAndCheck (str, projects) { } const project = words[0].trim(); const section = words[1].trim(); - if (isExist(project, projects)) { + if (projects.includes(project)) { return project; - } else if (isExist(section, projects)) { + } else if (projects.includes(section)) { return section; } return str; } - -function isExist(str, arr) { - return arr.some((p) => p === str); -}