Skip to content

Commit

Permalink
variant-ally: rewrite anchors with page variant
Browse files Browse the repository at this point in the history
This is better
  • Loading branch information
diskdance committed Jul 21, 2023
1 parent 3bd796c commit f9811d7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
4 changes: 2 additions & 2 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
## 是如何解决的?
本小工具的功能分为**变体管理****自动变体跳转****自动变体链接修正**三部分。

对于**有经验的用户**自动变体跳转和自动变体链接修正会关闭。以下用户为有经验的用户
对于**资深用户**自动变体跳转会关闭,而自动变体链接修正会保持开启。定义以下用户为资深用户
- 延伸确认用户
- WMF 员工和其他维基媒体组织成员
- 特定列表中的用户
Expand All @@ -42,7 +42,7 @@
- 站内导航。技术上通过 `document.referrer` 确定,如果用户的浏览器禁用了 Referrer,可能无法正确判断。

### 自动变体链接修正
如果当前页面的 URL 中指定了变体(如 `/zh-cn/条目``/w/index.php?title=条目&variant=zh-cn`),而用户点击了未指定变体的内链(如 `/wiki/条目`),那么本小工具会自动修正链接为偏好变体的版本
如果当前页面的 URL 中指定了变体(如 `/zh-cn/条目``/w/index.php?title=条目&variant=zh-cn`),而用户点击了未指定变体的内链(如 `/wiki/条目`),那么本小工具会自动修正链接为当前页面的变体。这样是为了让用户通过下拉菜单手动切换变体时,变体选择可以“蔓延”至新页面

## 不会解决的问题
1. 本小工具不涉及桌面移动端跳转相关逻辑。
Expand Down
30 changes: 14 additions & 16 deletions gadgets/variant-ally/src/controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { output } from './debug';
import { getPageVariant } from './model';

// Including:
// - w.wiki
Expand Down Expand Up @@ -76,6 +75,7 @@ function redirect(
function checkThisPage(
preferredVariant: string,
normalizationTargetVariant: string | null,
pageVariant: string,
): void {
const referrerHostname = new URL(document.referrer || DUMMY_REFERRER).hostname;
if (referrerHostname === location.hostname
Expand All @@ -85,9 +85,6 @@ function checkThisPage(
return;
}

// Non-wikitext pages are rejected in index.ts
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const pageVariant = getPageVariant()!;
if (pageVariant !== preferredVariant) {
output(() => ['checkThisPage', `Redirecting to ${preferredVariant}...`]);
redirect(preferredVariant, normalizationTargetVariant);
Expand All @@ -97,7 +94,7 @@ function checkThisPage(
}

function rewriteAnchors(
preferredVariant: string,
variant: string,
normalizationTargetVariant: string | null,
): void {
['click', 'auxclick', 'dragstart'].forEach((name) => {
Expand All @@ -113,17 +110,18 @@ function rewriteAnchors(
// Minerva/MobileFrontend: in .suggested-languages
// Monobook: in .pBody
if (anchor.closest('#p-variants, #p-variants-desktop, .suggested-languages, .pBody')) {
output(() => ['redirectAnchors', `Anchor is in variant dropdown list. Do nothing.`]);
output(() => ['redirectAnchors', `Anchor is in variant dropdown list. Stop.`]);
return;
}

const newLink = rewriteLink(anchor.href, preferredVariant, normalizationTargetVariant);
const newLink = rewriteLink(anchor.href, variant, normalizationTargetVariant);
if (ev instanceof DragEvent && ev.dataTransfer) {
// Modify drag data directly because setting href has no effect in drag event
for (const type of ev.dataTransfer.types) {
ev.dataTransfer.setData(type, newLink);
}
output(() => ['redirectAnchors', 'drag-handler', `Drop data changed!`]);
ev.dataTransfer.types.forEach((type) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
ev.dataTransfer!.setData(type, newLink);
});
output(() => ['redirectAnchors', 'dragHandler', `Drop data changed!`]);
} else {
// Use a mutex to avoid being overwritten by overlapped handler calls
if (anchor.dataset.vaMutex === undefined) {
Expand All @@ -133,8 +131,8 @@ function rewriteAnchors(
anchor.href = newLink;
output(() => [
'redirectAnchors',
'click-handler',
`href ${anchor.href}, origHref ${anchor.dataset.origHref}`,
'clickHandler',
`href ${anchor.href}, origLink ${origLink}`,
]);

// HACK: workaround popups not working on modified links
Expand All @@ -143,9 +141,9 @@ function rewriteAnchors(
anchor.addEventListener(innerName, (innerEv) => {
output(() => [
'redirectAnchors',
'click-handler',
'restoration-handler',
`Event ${innerEv.type} on ${anchor.href}, origHref ${anchor.dataset.origHref}`,
'clickHandler',
'restorationHandler',
`Event ${innerEv.type} on ${anchor.href}, origLink ${origLink}`,
]);

if (anchor.dataset.vaMutex !== undefined) {
Expand Down
15 changes: 8 additions & 7 deletions gadgets/variant-ally/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { output, showDebugInformation } from './debug';
import { checkThisPage, rewriteAnchors, showDialog } from './controller';
import { calculatePreferredVariant, getMediaWikiVariant } from './model';
import { calculatePreferredVariant, getMediaWikiVariant, getPageVariant } from './model';
import { isExperiencedUser, isWikitextPage } from './utils';

showDebugInformation();
Expand All @@ -13,13 +13,14 @@ if (!isWikitextPage()) {
output(() => ['index', 'Preferred variant is null, show variant dialog']);
showDialog();
} else {
if (isExperiencedUser()) {
output(() => ['index', 'Experienced user. Stop.']);
} else {
const normalizationTargetVariant = getMediaWikiVariant();
checkThisPage(preferredVariant, normalizationTargetVariant);
rewriteAnchors(preferredVariant, normalizationTargetVariant);
const normalizationTargetVariant = getMediaWikiVariant();
// For a wikitext page pageVariant cannot be null
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const pageVariant = getPageVariant()!;
if (!isExperiencedUser()) {
checkThisPage(preferredVariant, normalizationTargetVariant, pageVariant);
}
rewriteAnchors(pageVariant, normalizationTargetVariant);
}
}

Expand Down
15 changes: 11 additions & 4 deletions gadgets/variant-ally/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,23 @@ function getBrowserVariant(): string | null {
}

/**
* Get the variant inferred by MediaWiki, if it's valid.
* Get the "natural" variant inferred by MediaWiki when the link doesn't specify a variant.
*
* Used in link normalization.
*
* Only return valid variants, null otherwise.
*
* @returns variant
*/
function getMediaWikiVariant(): string | null {
return getAccountVariant() || getBrowserVariant();
return getAccountVariant() ?? getBrowserVariant();
}

/**
* Calculate preferred variant from Special:Preferences (logged-in users)
* or local storage (anonymous users). Resets local storage if there's a conflict.
* Calculate preferred variant from browser variant, local variant and account variant.
*
* Priority: account variant > local variant > browser variant
*
* @returns preferred variant
*/
function calculatePreferredVariant(): string | null {
Expand Down

0 comments on commit f9811d7

Please sign in to comment.