Wikipedia multi language view

View a Wikipedia entry with two (or more?) languages side by side for comparison and language learning.

  1. // ==UserScript==
  2. // @name Wikipedia multi language view
  3. // @name:zh Wikipedia 多语言浏览
  4. // @namespace https://userscript.snomiao.com/
  5. // @author snomiao@gmail.com
  6. // @version 0.0.4
  7. // @description View a Wikipedia entry with two (or more?) languages side by side for comparison and language learning.
  8. // @description:zh 以并列多语言视角浏览维基百科
  9. // @match https://*.wikipedia.org/wiki/*
  10. // @match https://zh.wikipedia.org/zh-*/*
  11. // @grant none
  12. // @run-at document-start
  13. // @license GPL-3.0+
  14. // @supportURL https://github.com/snomiao/userscript.js/issues
  15. // @contributionURL https://snomiao.com/donate
  16. // ==/UserScript==
  17. //
  18. // ref:
  19. // [javascript - Resize Cross Domain Iframe Height - Stack Overflow]( https://stackoverflow.com/questions/22086722/resize-cross-domain-iframe-height )
  20. //
  21.  
  22. if (location.hash.match("#langIfr")) {
  23. // iframe code send height
  24. const sendHeight = () =>
  25. parent.postMessage?.(
  26. { langIfr: { height: document.body.scrollHeight } },
  27. "*"
  28. );
  29. window.addEventListener("resize", sendHeight, false);
  30. window.addEventListener("load", sendHeight, false);
  31. sendHeight();
  32. } else {
  33. // parent code recv iframe's height
  34. const msgHandler = (e) => {
  35. const setHeight = (height) =>
  36. height &&
  37. document.querySelector("#langIfr")?.setAttribute("height", height);
  38. setHeight(e.data?.langIfr?.height);
  39. };
  40. window.addEventListener("message", msgHandler, false);
  41. // load iframe
  42. const langLnksGet = () =>
  43. Object.fromEntries(
  44. [...document.querySelectorAll("a.interlanguage-link-target")]
  45. .map((e) => ({
  46. lang: e.getAttribute("lang"),
  47. href: e.href,
  48. language: e.textContent,
  49. }))
  50. .map((e) => [e.lang, e])
  51. );
  52. const exlangFrameLoad = () => {
  53. const langLnks = langLnksGet();
  54. const langIframeLoad = (lang = "en") => {
  55. if (!langLnks[lang]) return false;
  56. document.body.setAttribute("style", "width: 50vw");
  57. document.body.querySelector("#langIfr")?.remove();
  58. document.querySelector("#sidebarCollapse")?.click();
  59. const langIfr = Object.assign(document.createElement("iframe"), {
  60. id: "langIfr",
  61. src: langLnks[lang].href + "#langIfr",
  62. });
  63. langIfr.setAttribute(
  64. "style",
  65. "border: none; position:absolute; left: 50vw; top: 0vh; width: 50vw"
  66. );
  67. document.body.appendChild(langIfr);
  68. return true;
  69. };
  70. langIframeLoad("en") || langIframeLoad("zh") || langIframeLoad("ja");
  71. };
  72. window.addEventListener("load", exlangFrameLoad, false);
  73. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址