From 7fa30fb7bf4a561853885ce2c3eafdc676f5a12e Mon Sep 17 00:00:00 2001 From: Ferryyer <217455109+Ferryyer@users.noreply.github.com> Date: Thu, 26 Nov 2026 19:53:34 -0800 Subject: [PATCH] LS-80: follow Feishu task page upgrades in place Co-authored-by: multica-agent --- scripts/feishu-tests/style.test.cjs | 90 +++++++++++++++++++- serverbox/assets/feishu/inspire-task.user.js | 52 ++++++++++- 2 files changed, 138 insertions(+), 4 deletions(-) diff --git a/scripts/feishu-tests/style.test.cjs b/scripts/feishu-tests/style.test.cjs index 5b3fec4..3cd0ec2 100644 --- a/scripts/feishu-tests/style.test.cjs +++ b/scripts/feishu-tests/style.test.cjs @@ -11,7 +11,7 @@ function page(path = '/next/task/my-tasks', html = fixture, origin = 'https://uq const {window} = dom; let tick; let frames = []; - window.__inspireTaskOptions = {enabled: true, ...options}; + if (options) window.__inspireTaskOptions = {enabled: true, ...options}; window.requestAnimationFrame = callback => { frames.push(callback); }; window.setInterval = callback => { tick = callback; }; window.matchMedia = () => ({matches: false, addEventListener() {}}); @@ -139,3 +139,91 @@ test('browser recommendation uses its close action, without hiding layout shells assert(!p.document.documentElement.hasAttribute('data-inspire-task-page')); } finally { p.close(); login.close(); early.close(); } }); + +function upgradeNotice(control) { + return `

检测到存在新版本页面 ${control}

`; +} + +test('the Feishu upgrade link opens the requested task in the existing view', () => { + const path = '/next/task/my-tasks?view=assigned#fixture'; + const p = page('/next/task/my-tasks', ''); + try { + p.document.body.innerHTML = upgradeNotice(`点击跳转`); + const link = p.document.querySelector('a'); + const navigations = []; + link.addEventListener('click', event => { + event.preventDefault(); + navigations.push({target: link.target, path: link.pathname + link.search + link.hash}); + }); + p.tick(); + p.tick(); + assert.deepEqual(navigations, [{target: '_self', path}]); + + p.window.history.replaceState({}, '', path); + p.document.body.innerHTML = fixture; + p.tick(); + assert(p.document.documentElement.hasAttribute('data-inspire-task')); + assert(p.document.querySelector('#inspire-task-nav-toggle')); + } finally { p.close(); } +}); + +test('the version prompt activates a page handler at most once across task documents', () => { + const p = page('/next/task/my-tasks', '', undefined, {embedded: false, enabled: false}); + try { + const notice = upgradeNotice('点击跳转'); + let activations = 0; + p.document.body.innerHTML = notice; + p.document.querySelector('span').onclick = () => activations++; + p.tick(); + assert.equal(activations, 1); + + // A full navigation re-runs the script while the WebView session persists. + delete p.window.InspireTaskAppearance; + p.document.body.innerHTML = notice; + p.document.querySelector('span').onclick = () => activations++; + p.window.eval(source); + p.tick(); + assert.equal(activations, 1); + assert(!p.document.documentElement.hasAttribute('data-inspire-task')); + } finally { p.close(); } +}); + +test('only the app follows the bare messages landing as a version upgrade', () => { + for (const native of [true, false]) { + const p = page('/next/task/my-tasks', '', undefined, native ? {embedded: true} : null); + try { + p.document.body.innerHTML = upgradeNotice('点击跳转'); + let activations = 0; + const link = p.document.querySelector('a'); + link.addEventListener('click', event => { event.preventDefault(); activations++; }); + p.tick(); + assert.equal(activations, native ? 1 : 0); + assert.equal(link.target, native ? '_self' : '_blank'); + } finally { p.close(); } + } +}); + +test('version prompts leave login, task content and unrecognized destinations alone', () => { + const pageCases = [ + ['/accounts/login', upgradeNotice('')], + ['/next/task/my-tasks', fixture + upgradeNotice('')], + ['/next/task/my-tasks', '

任务已归档

'], + ['/next/task/my-tasks', upgradeNotice('点击跳转')], + ['/next/task/my-tasks', upgradeNotice('点击跳转')], + ['/next/task/my-tasks', upgradeNotice('点击跳转')], + ['/next/task/my-tasks', upgradeNotice('点击跳转')], + ]; + for (const [path, html] of pageCases) { + const p = page(path, ''); + try { + let activations = 0; + p.document.body.innerHTML = html; + p.document.addEventListener('click', event => { + event.preventDefault(); + activations++; + }); + p.tick(); + assert.equal(activations, 0, html); + } finally { p.close(); } + } +}); diff --git a/serverbox/assets/feishu/inspire-task.user.js b/serverbox/assets/feishu/inspire-task.user.js index 800f434..4c2257b 100644 --- a/serverbox/assets/feishu/inspire-task.user.js +++ b/serverbox/assets/feishu/inspire-task.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name Inspire Queue · 飞书任务 // @namespace local.feishu.task -// @version 2.0.4 +// @version 2.0.5 // @description 隐藏全局导航,暖白与陶土色主题,小窗口可展开任务导航。 // @match https://uqyv5nolng8.feishu.cn/* // @grant GM_getValue @@ -16,9 +16,12 @@ if (location.origin !== 'https://uqyv5nolng8.feishu.cn' || window.top !== window) return; if (window.InspireTaskAppearance) return; const attribute = 'data-inspire-task'; + const taskPath = /^\/next\/task(?:\/|$)/; + const upgradeAttemptKey = 'inspire-task-page-upgrade-attempted'; const native = window.__inspireTaskOptions; const embedded = native?.embedded === true; const closeAttempts = new WeakMap(); + let followedUpgrade = false; let resizePending = false; let enabled = native ? native.enabled !== false : (typeof GM_getValue === 'function' ? GM_getValue('hideGlobalNav', true) : true); @@ -123,6 +126,46 @@ })); } function closeNav() { open = false; sync(); } + function followPageUpgrade() { + if (!document.body) return; + let storage; + try { storage = window.sessionStorage; } catch { return; } + const attempted = storage.getItem(upgradeAttemptKey) !== null; + if (document.querySelector('#root-todo .task-view-toolbar')) { + if (attempted) storage.removeItem(upgradeAttemptKey); + return; + } + const notice = '检测到存在新版本页面'; + if (attempted || followedUpgrade || !document.body.textContent.includes(notice)) return; + + const action = '点击跳转'; + const controls = 'a, button, [role="link"], [role="button"]'; + const textOf = element => element?.textContent.replace(/\s+/g, ''); + const label = [...document.body.querySelectorAll(`${controls}, span`)].find(element => + textOf(element) === action && + !element.closest('form, [contenteditable], [hidden], [aria-hidden="true"]') && + [element.parentElement, element.parentElement?.parentElement].some(parent => + textOf(parent) === notice + action)); + if (!label) return; + const control = label.closest(controls) ?? label; + if (control.matches(':disabled, [aria-disabled="true"], [download]')) return; + + const hasHref = control.hasAttribute('href'); + if (hasHref) { + let target; + try { target = new URL(control.href); } catch { return; } + if (target.origin !== location.origin || target.username || target.password) return; + // Only the app has native recovery from the bare messages landing. + const messenger = /^\/next\/messenger\/?$/.test(target.pathname) && + target.href === target.origin + target.pathname; + if (!taskPath.test(target.pathname) && !(native && messenger)) return; + } + // The marker survives a document reload, including Feishu returning the same notice. + try { storage.setItem(upgradeAttemptKey, '1'); } catch { return; } + followedUpgrade = true; + if (hasHref) control.setAttribute('target', '_self'); + control.click(); + } function hideBrowserSuggestion() { if (!document.body) return; const shell = 'body, main, [id^="root"], .appLayout, .messagesLayout, .appLayout-content, .larklet-extension, .larklet-extension-component'; @@ -199,11 +242,14 @@ const html = document.documentElement; if (!html) return; if (!style.isConnected) html.append(style); - const taskPage = /^\/next\/task(?:\/|$)/.test(location.pathname); + const taskPage = taskPath.test(location.pathname); const active = enabled && taskPage; html.toggleAttribute('data-inspire-task-page', taskPage); html.toggleAttribute('data-inspire-embedded', embedded); - if (taskPage) hideBrowserSuggestion(); + if (taskPage) { + followPageUpgrade(); + hideBrowserSuggestion(); + } const wasActive = html.hasAttribute(attribute); html.toggleAttribute(attribute, active); html.toggleAttribute('data-inspire-dark', active && dark);