VRChat Web Pages Extender

Add features into VRChat Web Pages and improve user experience.

当前为 2019-12-31 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name VRChat Web Pages Extender
  3. // @name:ja VRChat Webページ拡張
  4. // @description Add features into VRChat Web Pages and improve user experience.
  5. // @description:ja VRChatのWebページに機能を追加し、また使い勝手を改善します。
  6. // @namespace https://greasyfork.org/users/137
  7. // @version 2.6.0
  8. // @match https://www.vrchat.com/*
  9. // @match https://vrchat.com/*
  10. // @match https://api.vrchat.cloud/*
  11. // @require https://greasyfork.org/scripts/17895/code/polyfill.js?version=625392
  12. // @require https://greasyfork.org/scripts/19616/code/utilities.js?version=230651
  13. // @license MPL-2.0
  14. // @contributionURL https://pokemori.booth.pm/items/969835
  15. // @compatible Edge 非推奨 / Deprecated
  16. // @compatible Firefox
  17. // @compatible Opera
  18. // @compatible Chrome
  19. // @grant dummy
  20. // @run-at document-start
  21. // @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgMAAADZCAQAAAAX6Pu8AAAVKklEQVR42u2dv3LjSHfFD7lTZWfkZhuZnCrnxAau2oxQtpkwT0BM5kycJxD1BOKkTkRlzgZ6AoGRnQlKXOVyIKjqC+xIZPZlcKCRVhIb3Q2gG2g0z4+RRBIk+8/Bvbdv3x6gKiEIIW6TVnv5QOtVY4QIEWDO9iWkJ2yRIUWKnQkZCBFjwTYlpKfcIMGmiQzEWGHCdiSk5zxiJZOCgcQKWGPG9iPEGymIy2IGw5K3rHFLESDEIya4xQZjXWtgjJQSQIiX3CNGppaBAClGbC1CPGWP8KMQDCkChBwVI6SIZNYARYCQI7QIBlVjAv/8yDYkxG3+9tvf/0EpBAFykQwkOC1/1wRLhAjYwoT0gBwJNriXveT+r+n8y+s/lzgre/0M/441/sBvbF1CesEYf+BfESJHqfn+GwYveQSD13fl4qjACCss2aqE9JQEMfZlT35+dgxeVgrWZSKQUgQI6TERsvKQ3+atNTDFg9gZSMVJR4SQHrFDWBYnOEH6Yg2sxJbAhiJ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J4y1z0mpDNR4gfxGVCcFo8/RSBp2JEGSDEazZlQjAproqieCoW4ucJId4wRlYmBJJHxoYjxCem2FWWgSWbjRC/CCvLwJSNRohvxJVEYMMGI+S4hWBHW4CQYxeCmE1FiM8xgh1FgJBjZ4xE6g5QBAg5EpsgLQkMNogJDNiuhPSMABFCTDEB8IgcCRLkTS74/x0F/GgNxI6KAAAAAElFTkSuQmCC
  22. // @author 100の人
  23. // @homepageURL https://pokemori.booth.pm/items/969835
  24. // ==/UserScript==
  25.  
  26. 'use strict';
  27.  
  28. // L10N
  29. Gettext.setLocalizedTexts({
  30. /*eslint-disable quote-props, max-len */
  31. 'en': {
  32. 'エラーが発生しました': 'Error occurred',
  33. '$LENGTH$ 文字まで表示可能です。': 'This text is displayed up to $LENGTH$ characters.',
  34. },
  35. /*eslint-enable quote-props, max-len */
  36. });
  37.  
  38. Gettext.setLocale(navigator.language);
  39.  
  40.  
  41.  
  42. if (typeof content !== 'undefined') {
  43. // For Greasemonkey 4
  44. fetch = content.fetch.bind(content); //eslint-disable-line no-global-assign, no-native-reassign, no-undef
  45. }
  46.  
  47.  
  48.  
  49. /**
  50. * ページ上部にエラー内容を表示します。
  51. * @param {Error} exception
  52. * @returns {void}
  53. */
  54. function showError(exception)
  55. {
  56. console.error(exception);
  57. try {
  58. const errorMessage = _('エラーが発生しました') + ': ' + exception
  59. + ('stack' in exception ? '\n\n' + exception.stack : '');
  60. const homeContent = document.getElementsByClassName('home-content')[0];
  61. if (homeContent) {
  62. homeContent.firstElementChild.firstElementChild.insertAdjacentHTML('afterbegin', h`<div class="row">
  63. <div class="alert alert-danger fade show" role="alert"
  64. style="white-space: pre-wrap; font-size: 1rem; font-weight: normal;">${errorMessage}</div>
  65. </div>`);
  66. } else {
  67. alert(errorMessage);
  68. }
  69. } catch (e) {
  70. alert(_('エラーが発生しました') + ': ' + e);
  71. }
  72. }
  73.  
  74. const ID = 'vrchat-web-pages-extender-137';
  75.  
  76. /**
  77. * 一度に取得できる最大の要素数。
  78. * @constant {number}
  79. */
  80. const MAX_ITEMS_COUNT = 100;
  81.  
  82. /**
  83. * Statusの種類。
  84. * @constant {number}
  85. */
  86. const STATUSES = {
  87. 'join me': {
  88. label: 'Join Me: Auto-accept join requests',
  89. color: '--status-joinme',
  90. },
  91. active: {
  92. label: 'Active: See friends’ notifications',
  93. color: '--status-online',
  94. },
  95. busy: {
  96. label: 'Busy: Don’t see friends’ notifications',
  97. color: '--status-busy',
  98. },
  99. };
  100.  
  101. /**
  102. * Status Descriptionの最大文字数。
  103. * @constant {number}
  104. */
  105. const MAX_STATUS_DESCRIPTION_LENGTH = 32;
  106.  
  107. /**
  108. * 一つのブックマークグループの最大登録数。
  109. * @constant {number}
  110. */
  111. const MAX_FAVORITES_COUNT_PER_GROUP = 32;
  112.  
  113. /**
  114. * 各ブックマークタグの既定名 (ユーザー設定名を取得する方法は不明)。
  115. * @constant {OObject.<Object.<string[]>>} {@link FAVORITE_TYPES}の要素をキーに持つ連想配列。
  116. */
  117. const DEFAULT_FAVORITE_TAG_NAMES = {
  118. friend: {
  119. group_0: 'Group 1',
  120. group_1: 'Group 2',
  121. group_2: 'Group 3',
  122. },
  123. world: {
  124. worlds0: 'Playlist 1',
  125. // worlds1: 'Playlist 2',
  126. worlds2: 'Playlist 2',
  127. worlds3: 'Playlist 3',
  128. worlds4: 'Playlist 4',
  129. },
  130. };
  131.  
  132. /**
  133. * @type {Function}
  134. * @access private
  135. */
  136. let resolveUserDetails;
  137.  
  138. /**
  139. * @type {Promise.<Object>}
  140. * @access private
  141. */
  142. const userDetails = new Promise(function (resolve) {
  143. resolveUserDetails = resolve;
  144. });
  145.  
  146. addEventListener('message', function receiveUserDetails(event) {
  147. if (event.origin !== location.origin || typeof event.data !== 'object' || event.data === null
  148. || event.data.id !== ID || !event.data.userDetails) {
  149. return;
  150. }
  151. event.currentTarget.removeEventListener(event.type, receiveUserDetails);
  152.  
  153. resolveUserDetails(event.data.userDetails);
  154. });
  155.  
  156. /**
  157. * ログインしているユーザーの情報を取得します。
  158. * @see [User Info — VRChat API Documentation]{@link https://vrchatapi.github.io/#/UserAPI/CurrentUserDetails}
  159. * @returns {Promise.<?Object.<(string|string[]|boolean|number|Object)>>}
  160. */
  161. async function getUserDetails()
  162. {
  163. return await userDetails;
  164. }
  165.  
  166. /**
  167. * スクリプトで扱うブックマークの種類。
  168. * @constant {string[]}
  169. */
  170. const FAVORITE_TYPES = ['friend', 'world'];
  171.  
  172. /**
  173. * @type {Promise.<Object.<(string|string[])[]>[]>}
  174. * @access private
  175. */
  176. let favorites;
  177.  
  178. /**
  179. * ブックマークを全件取得します。
  180. * @see [List Favorites — VRChat API Documentation]{@link https://vrchatapi.github.io/#/FavoritesAPI/ListAllFavorites}
  181. * @returns {Promise.<Object.<(string|string[])[]>[]>} {@link FAVORITE_TYPES}の要素をキーに持つ連想配列。
  182. */
  183. function getFavorites()
  184. {
  185. return favorites || (favorites = async function () {
  186. const allFavorites = { };
  187. for (const type of FAVORITE_TYPES) {
  188. allFavorites[type] = [];
  189. }
  190. let offset = 0;
  191. while (true) {
  192. const favorites
  193. = await fetch(`/api/1/favorites/?n=${MAX_ITEMS_COUNT}&offset=${offset}`, {credentials: 'same-origin'})
  194. .then(async response => response.ok ? response.json() : Promise.reject(
  195. new Error(`${response.status} ${response.statusText}\n${await response.text()}`)
  196. ))
  197. .catch(showError);
  198.  
  199. for (const favorite of favorites) {
  200. if (!FAVORITE_TYPES.includes(favorite.type)) {
  201. continue;
  202. }
  203. allFavorites[favorite.type].push(favorite);
  204. }
  205.  
  206. if (favorites.length < MAX_ITEMS_COUNT) {
  207. break;
  208. }
  209.  
  210. offset += favorites.length;
  211. }
  212. return allFavorites;
  213. }());
  214. }
  215.  
  216. /**
  217. * 「Edit Profile」ページに、ステータス文変更フォームを挿入します。
  218. * @returns {Promise.<void>}
  219. */
  220. async function insertUpdateStatusForm()
  221. {
  222. if ('update-status' in document.forms) {
  223. return;
  224. }
  225.  
  226. const sidebarStatus = document.querySelector('.leftbar .user-info h6 span[title]');
  227. const sidebarStatusDescription = document.querySelector('.leftbar .statusDescription small');
  228.  
  229. const templateCard = document.getElementById('name-change-submit').closest('.card');
  230. const card = templateCard.cloneNode(true);
  231. card.getElementsByClassName('card-header')[0].textContent = 'Status';
  232. const form = card.getElementsByTagName('form')[0];
  233. form.name = 'update-status';
  234. form.action = '/api/1/users/' + document.querySelector('[href*="/home/user/usr_"]').pathname.replace(/.+\//u, '');
  235.  
  236. const description = form.displayName;
  237. description.id = 'status-description';
  238. description.type = 'text';
  239. description.name = 'statusDescription';
  240. description.value = sidebarStatusDescription.textContent;
  241. description.placeholder = '';
  242. description.pattern = `.{0,${MAX_STATUS_DESCRIPTION_LENGTH}}`;
  243. description.title = _('$LENGTH$ 文字まで表示可能です。').replace('$LENGTH$', MAX_STATUS_DESCRIPTION_LENGTH);
  244. description.parentElement.getElementsByClassName('alert')[0].remove();
  245. const descriptionContainer = description.closest('.col-10');
  246. descriptionContainer.classList.replace('col-10', 'col');
  247.  
  248. descriptionContainer.parentElement.classList.add('mb-2');
  249. const statusContainer = descriptionContainer.previousElementSibling;
  250. statusContainer.outerHTML = '<div class="col-auto"><select name="status" class="form-control">'
  251. + Object.keys(STATUSES).map(status => h`<option value="${status}">⬤ ${STATUSES[status].label}</option>`)
  252. .join('')
  253. + '</select></div>';
  254. form.status.value = sidebarStatus.title;
  255. form.status.classList.add(form.status.value.replace(' ', '-'));
  256. form.status.addEventListener('change', function (event) {
  257. const classList = event.target.classList;
  258. classList.remove(...Object.keys(STATUSES).map(status => status.replace(' ', '-')));
  259. classList.add(event.target.value.replace(' ', '-'));
  260. });
  261.  
  262. const submit = form.getElementsByClassName('btn')[0];
  263. submit.id = 'status-change-submit';
  264. submit.textContent = 'Update Status';
  265. submit.disabled = false;
  266. form.addEventListener('submit', function (event) {
  267. event.preventDefault();
  268. for (const control of event.target) {
  269. control.disabled = true;
  270. }
  271.  
  272. const body = {};
  273. for (const element of event.target) {
  274. if (element.localName === 'button') {
  275. continue;
  276. }
  277. body[element.name] = element.value;
  278. }
  279.  
  280. fetch(event.target.action, {
  281. method: 'PUT',
  282. headers: {'content-type': 'application/json'},
  283. credentials: 'same-origin',
  284. body: JSON.stringify(body),
  285. })
  286. .then(async function (response) {
  287. if (!response.ok) {
  288. return Promise.reject(
  289. new Error(`${response.status} ${response.statusText}\n${await response.text()}`)
  290. );
  291. }
  292. sidebarStatus.classList.remove(...Object.keys(STATUSES).map(status => status.replace(' ', '-')));
  293. sidebarStatus.classList.add(event.target.status.value.replace(' ', '-'));
  294. sidebarStatus.title = event.target.status;
  295. sidebarStatusDescription.textContent = event.target.statusDescription.value;
  296. })
  297. .catch(showError)
  298. .then(function () {
  299. for (const control of event.target) {
  300. control.disabled = false;
  301. }
  302. });
  303. });
  304.  
  305. templateCard.parentElement.getElementsByClassName('card')[0].before(card, document.createElement('hr'));
  306. }
  307.  
  308. /**
  309. * ブックマーク登録/解除ボタンの登録数表示を更新します。
  310. * @param {string} type - 「user」「favorite」のいずれか。
  311. * @returns {Promise.<void>}
  312. */
  313. async function updateFavoriteCounts(type)
  314. {
  315. const counts = {};
  316. for (const favorite of (await getFavorites())[type]) {
  317. for (const tag of favorite.tags) {
  318. if (!(tag in counts)) {
  319. counts[tag] = 0;
  320. }
  321. counts[tag]++;
  322. }
  323. }
  324.  
  325. for (const button of document.getElementsByName('favorite-' + type)) {
  326. button.getElementsByClassName('count')[0].textContent = counts[button.value] || 0;
  327. }
  328. }
  329.  
  330. /**
  331. * ブックマーク登録/解除ボタンを追加します。
  332. * @param {string} type - {@link FAVOLITE_TYPES}のいずれかの要素。
  333. * @returns {Promise.<void>}
  334. */
  335. async function insertFavoriteButtons(type)
  336. {
  337. const homeContent = document.getElementsByClassName('home-content')[0];
  338. const sibling = type === 'friend'
  339. ? homeContent.getElementsByClassName('btn-group-vertical')[0]
  340. : homeContent.querySelector('[href*="/home/launch"]');
  341. if (!sibling) {
  342. return;
  343. }
  344. const parent = sibling.closest('[class*="col-"]');
  345.  
  346. const result = /[a-z]+_[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/.exec(location.pathname);
  347. if (!result) {
  348. return;
  349. }
  350. const id = result[0];
  351.  
  352. const buttons = document.getElementsByName('favorite-' + type);
  353. if (type === 'friend' && !(await getUserDetails()).friends.includes(id) || buttons[0]) {
  354. return;
  355. }
  356.  
  357. parent.insertAdjacentHTML('beforeend', '<div role="group" class="w-100 btn-group-lg btn-group-vertical mt-4">'
  358. + Object.keys(DEFAULT_FAVORITE_TAG_NAMES[type]).sort().map(tag => h`<button type="button"
  359. class="btn btn-secondary" name="favorite-${type}" value="${tag}" disabled="">
  360. <span aria-hidden="true" class="fa fa-star"></span>
  361. &#xA0;<span class="name">${DEFAULT_FAVORITE_TAG_NAMES[type][tag]}</span>
  362. &#xA0;<span class="count">‒</span>⁄${MAX_FAVORITES_COUNT_PER_GROUP}
  363. </button>`).join('')
  364. + '</div>');
  365.  
  366. await updateFavoriteCounts(type);
  367.  
  368. const tags = [].concat(...(await getFavorites())[type]
  369. .filter(favorite => favorite.favoriteId === id)
  370. .map(favorite => favorite.tags));
  371.  
  372. for (const button of buttons) {
  373. button.dataset.id = id;
  374. if (tags.includes(button.value)) {
  375. button.classList.remove('btn-secondary');
  376. button.classList.add('btn-primary');
  377. }
  378. if (button.classList.contains('btn-primary')
  379. || button.getElementsByClassName('count')[0].textContent < MAX_FAVORITES_COUNT_PER_GROUP) {
  380. button.disabled = false;
  381. }
  382. }
  383.  
  384. parent.lastElementChild.addEventListener('click', async function (event) {
  385. if (event.target.name !== 'favorite-' + type) {
  386. return;
  387. }
  388.  
  389. const buttons = document.getElementsByName('favorite-' + type);
  390. for (const button of buttons) {
  391. button.disabled = true;
  392. }
  393.  
  394. const id = event.target.dataset.id;
  395. const newTags = event.target.classList.contains('btn-secondary') ? [event.target.value] : [];
  396.  
  397. const favorites = (await getFavorites())[type];
  398. for (let i = favorites.length - 1; i >= 0; i--) {
  399. if (favorites[i].favoriteId === id) {
  400. await fetch(
  401. '/api/1/favorites/' + favorites[i].id,
  402. {method: 'DELETE', credentials: 'same-origin'}
  403. );
  404.  
  405. for (const button of buttons) {
  406. if (favorites[i].tags.includes(button.value)) {
  407. button.classList.remove('btn-primary');
  408. button.classList.add('btn-secondary');
  409. }
  410. }
  411.  
  412. favorites.splice(i, 1);
  413. }
  414. }
  415.  
  416. if (newTags.length > 0) {
  417. await fetch('/api/1/favorites', {
  418. method: 'POST',
  419. headers: { 'content-type': 'application/json' },
  420. credentials: 'same-origin',
  421. body: JSON.stringify({type, favoriteId: id, tags: newTags}),
  422. })
  423. .then(async response => response.ok ? response.json() : Promise.reject(
  424. new Error(`${response.status} ${response.statusText}\n${await response.text()}`)
  425. ))
  426. .then(function (favorite) {
  427. favorites.push(favorite);
  428. for (const button of buttons) {
  429. if (favorite.tags.includes(button.value)) {
  430. button.classList.remove('btn-secondary');
  431. button.classList.add('btn-primary');
  432. }
  433. }
  434. })
  435. .catch(showError);
  436. }
  437.  
  438. await updateFavoriteCounts(type);
  439.  
  440. for (const button of buttons) {
  441. if (button.getElementsByClassName('count')[0].textContent < MAX_FAVORITES_COUNT_PER_GROUP) {
  442. button.disabled = false;
  443. }
  444. }
  445. });
  446. }
  447.  
  448. /**
  449. * フレンド一覧で次のページが確実に存在しない場合、次ページのボタンを無効化します。
  450. * また、オンラインのフレンド数を表示します。
  451. * @param {HTMLDivElement} group 「friend-group」クラスを持つ要素。
  452. * @returns {void}
  453. */
  454. function improveFriendList(group)
  455. {
  456. // フレンド一覧で次のページが確実に存在しない場合、次ページのボタンを無効化します
  457. const pager = group.querySelector('.friend-group > div:last-of-type');
  458. const count = group.querySelectorAll('.friend-group > div:not(:last-of-type)').length;
  459. const nextPageButton = pager.getElementsByClassName('fa-angle-down')[0].closest('button');
  460. nextPageButton.disabled = count < MAX_ITEMS_COUNT;
  461.  
  462. // オンラインのフレンド数を表示します
  463. const heading = group.firstElementChild;
  464. if (heading.textContent.includes('Online')) {
  465. heading.textContent = `Online (${
  466. MAX_ITEMS_COUNT * (/[0-9]+/.exec(pager.getElementsByClassName('page')[0].textContent)[0] - 1)
  467. + count
  468. + (nextPageButton.disabled ? '' : '+')
  469. })`;
  470. }
  471. }
  472.  
  473. /**
  474. * ページ読み込み後に一度だけ実行する処理をすでに行っていれば `true`。
  475. * @type {boolean}
  476. * @access private
  477. */
  478. let headChildrenInserted = false;
  479.  
  480. new MutationObserver(async function (mutations) {
  481. if (document.head && !headChildrenInserted) {
  482. headChildrenInserted = true;
  483. document.head.insertAdjacentHTML('beforeend', `<style>
  484. /*====================================
  485. Edit Profile
  486. */
  487. ` + Object.keys(STATUSES).map(status => `
  488. [name="status"].${CSS.escape(status.replace(' ', '-'))},
  489. [name="status"] option[value=${CSS.escape(status)}] {
  490. color: var(${STATUSES[status].color});
  491. }
  492. `).join('') + `
  493.  
  494. /*====================================
  495. フレンドのユーザーページ
  496. */
  497. .btn[name^="favorite-"] {
  498. white-space: unset;
  499. }
  500. </style>`);
  501.  
  502. // ユーザー情報を取得します
  503. // ページ名を改善します
  504. GreasemonkeyUtils.executeOnUnsafeContext(function (id) {
  505.  
  506. const responseText = Object.getOwnPropertyDescriptor(XMLHttpRequest.prototype, 'responseText');
  507. responseText.get = new Proxy(responseText.get, {
  508. apply(get, thisArgument, argumentList)
  509. {
  510. const responseText = Reflect.apply(get, thisArgument, argumentList);
  511. if (thisArgument.status === 200
  512. && new URL(thisArgument.responseURL).pathname === '/api/1/auth/user') {
  513. postMessage({ id, userDetails: JSON.parse(responseText) }, location.origin);
  514. }
  515. return responseText;
  516. },
  517. });
  518. Object.defineProperty(XMLHttpRequest.prototype, 'responseText', responseText);
  519.  
  520. History.prototype.pushState = new Proxy(History.prototype.pushState, {
  521. apply(pushState, thisArgument, argumentList)
  522. {
  523. Reflect.apply(pushState, thisArgument, argumentList);
  524. document.title = document.title.split(' | ').slice(-1)[0];
  525. },
  526. });
  527. }, [ ID ]);
  528.  
  529. addEventListener('popstate', function () {
  530. document.title = document.title.split(' | ').slice(-1)[0];
  531. });
  532. }
  533.  
  534. for (const mutation of mutations) {
  535. let parent = mutation.target;
  536. if (parent.id === 'home') {
  537. break;
  538. }
  539.  
  540. if (/* URLを開いたとき */ parent.localName === 'head' && document.body
  541. || /* ページを移動したとき */ parent.id === 'app' || parent.classList.contains('home-content')
  542. || parent.parentElement && parent.parentElement.classList.contains('home-content')) {
  543. const homeContent = document.getElementsByClassName('home-content')[0];
  544. if (!homeContent || homeContent.getElementsByClassName('fa-cog')[0]) {
  545. break;
  546. }
  547.  
  548. let promise;
  549. if (location.pathname === '/home/profile') {
  550. // 「Edit Profile」ページなら
  551. promise = insertUpdateStatusForm();
  552. } else if (location.pathname.startsWith('/home/user/')) {
  553. // ユーザーページ
  554. promise = insertFavoriteButtons('friend');
  555. if (!document.title.includes('|')) {
  556. const displayName = document.getElementsByTagName('h2')[0].textContent;
  557. const name = document.getElementsByTagName('h3')[0].firstChild.data;
  558. document.title = `${displayName} ${name} | ${document.title}`;
  559. }
  560. } else if (location.pathname.startsWith('/home/world/')) {
  561. // ワールドページ
  562. promise = insertFavoriteButtons('world');
  563. if (!document.title.includes('|')) {
  564. const heading = document.querySelector('.home-content h3');
  565. const name = heading.firstChild.data;
  566. const author = heading.getElementsByTagName('small')[0].textContent;
  567. document.title = `${name} ${author} | ${document.title}`;
  568. }
  569. }
  570. if (promise) {
  571. promise.catch(showError);
  572. }
  573. }
  574.  
  575. if (parent.classList.contains('friend-container')) {
  576. parent = mutation.addedNodes[0];
  577. }
  578.  
  579. if (parent.classList.contains('friend-group')) {
  580. let groups = document.getElementsByClassName('friend-group');
  581. const heading = groups[0].firstElementChild.textContent;
  582. if (groups.length === 1 || heading.includes('Online') && !heading.includes('(')) {
  583. groups = [parent];
  584. }
  585.  
  586. for (const group of groups) {
  587. improveFriendList(group);
  588. }
  589. break;
  590. }
  591. }
  592. }).observe(document, {childList: true, subtree: true});