Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 89 additions & 1 deletion scripts/feishu-tests/style.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}});
Expand Down Expand Up @@ -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 `<main><p>检测到存在新版本页面 ${control}</p></main>`;
}

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(`<a href="${path}" target="_blank">点击跳转</a>`);
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('<span>点击跳转</span>');
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('<a href="/next/messenger" target="_blank">点击跳转</a>');
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('<button>点击跳转</button>')],
['/next/task/my-tasks', fixture + upgradeNotice('<button>点击跳转</button>')],
['/next/task/my-tasks', '<p>任务已归档 <button>点击跳转</button></p>'],
['/next/task/my-tasks', upgradeNotice('<a href="https://example.com/next/task/my-tasks">点击跳转</a>')],
['/next/task/my-tasks', upgradeNotice('<a href="https://example.com/next/task/my-tasks"><span>点击跳转</span></a>')],
['/next/task/my-tasks', upgradeNotice('<a href="/accounts/login">点击跳转</a>')],
['/next/task/my-tasks', upgradeNotice('<a href="/next/messenger?openChatId=fixture">点击跳转</a>')],
];
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(); }
}
});
52 changes: 49 additions & 3 deletions serverbox/assets/feishu/inspire-task.user.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down
Loading