LinkedIn Tool

Minor enhancements to LinkedIn. Mostly just hotkeys.

当前为 2023-08-03 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name LinkedIn Tool
  3. // @namespace dalgoda@gmail.com
  4. // @match https://www.linkedin.com/*
  5. // @version 1.4.0
  6. // @author Mike Castle
  7. // @description Minor enhancements to LinkedIn. Mostly just hotkeys.
  8. // @license GPL-3.0-or-later; https://www.gnu.org/licenses/gpl-3.0.txt
  9. // @supportURL https://github.com/nexushoratio/userscripts/blob/main/linkedin-tool.md
  10. // @require https://cdn.jsdelivr.net/npm/@violentmonkey/shortcut@1
  11. // @require https://cdn.jsdelivr.net/npm/@violentmonkey/dom@2
  12. // ==/UserScript==
  13.  
  14. /* global VM */
  15.  
  16. (function () {
  17. 'use strict';
  18.  
  19. console.debug('Parsing successful.');
  20.  
  21. // I'm lazy. The version of emacs I'm using does not support
  22. // #private variables out of the box, so using underscores until I
  23. // get a working configuration.
  24. class Page {
  25. // The immediate following can be set if derived classes
  26.  
  27. // What pathname part of the URL this page should handle. The
  28. // special case of null is used by the Pages class to represent
  29. // global keys.
  30. _pathname;
  31.  
  32. // CSS selector for capturing clicks on this page. If overridden,
  33. // then the class should also provide a _clickHandler() method.
  34. _click_handler_selector = null;
  35.  
  36. // List of keystrokes to register automatically. They are objects
  37. // with keys of `seq`, `desc`, and `func`. The `seq` is used to
  38. // define they keystroke sequence to trigger the function. The
  39. // `desc` is used to create the help screen. The `func` is a
  40. // function, usually in the form of `this.methodName`. The
  41. // function is bound to `this` before registering it with
  42. // VM.shortcut.
  43. _auto_keys = [];
  44.  
  45. // Private members.
  46.  
  47. _keyboard = new VM.shortcut.KeyboardService();
  48.  
  49. // Tracks which HTMLElement holds the `onclick` function.
  50. _click_handler_element = null;
  51.  
  52. // Magic for VM.shortcut. This disables keys when focus is on an
  53. // input type field.
  54. static _navOption = {
  55. caseSensitive: true,
  56. condition: '!inputFocus',
  57. };
  58.  
  59. constructor() {
  60. this._boundClickHandler = this._clickHandler.bind(this);
  61. }
  62.  
  63. start() {
  64. for (const {seq, func} of this._auto_keys) {
  65. this._addKey(seq, func.bind(this));
  66. }
  67. }
  68.  
  69. get pathname() {
  70. return this._pathname;
  71. }
  72.  
  73. get keyboard() {
  74. return this._keyboard;
  75. }
  76.  
  77. activate() {
  78. this._keyboard.enable();
  79. this._enableClickHandler();
  80. }
  81.  
  82. deactivate() {
  83. this._keyboard.disable();
  84. this._disableClickHandler();
  85. }
  86.  
  87. get helpHeader() {
  88. return this.constructor.name;
  89. }
  90.  
  91. get helpContent() {
  92. return this._auto_keys;
  93. }
  94.  
  95. _addKey(seq, func) {
  96. this._keyboard.register(seq, func, Page._navOption);
  97. }
  98.  
  99. _enableClickHandler() {
  100. if (this._click_handler_selector) {
  101. // Page is dynamically building, so keep watching it until the
  102. // element shows up.
  103. VM.observe(document.body, () => {
  104. const element = document.querySelector(this._click_handler_selector);
  105. if (element) {
  106. this._click_handler_element = element;
  107. this._click_handler_element.addEventListener('click', this._boundClickHandler);
  108.  
  109. return true;
  110. }
  111. });
  112. }
  113. }
  114.  
  115. _disableClickHandler() {
  116. if (this._click_handler_element) {
  117. this._click_handler_element.removeEventListener('click', this._boundClickHandler);
  118. this._click_handler_element = null
  119. }
  120. }
  121.  
  122. // Override this function in derived classes that want to react to
  123. // random clicks on a page, say to update current element in
  124. // focus.
  125. _clickHandler(evt) {
  126. alert(`Found a bug! ${this.constructor.name} wants to handle clicks, but forgot to create a handler.`);
  127. }
  128.  
  129. }
  130.  
  131. class Global extends Page {
  132. _pathname = null;
  133. _auto_keys = [
  134. {seq: '?', desc: 'Show keyboard help', func: this._help},
  135. {seq: '/', desc: 'Go to Search box', func: this._gotoSearch},
  136. {seq: 'g h', desc: 'Go Home (aka, Feed)', func: this._goHome},
  137. {seq: 'g m', desc: 'Go to My Network', func: this._gotoMyNetwork},
  138. {seq: 'g j', desc: 'Go to Jobs', func: this._gotoJobs},
  139. {seq: 'g g', desc: 'Go to Messaging', func: this._gotoMessaging},
  140. {seq: 'g n', desc: 'Go to Notifications', func: this._gotoNotifications},
  141. {seq: 'g p', desc: 'Go to Profile (aka, Me)', func: this._gotoProfile},
  142. {seq: 'g b', desc: 'Go to Business', func: this._gotoBusiness},
  143. {seq: 'g l', desc: 'Go to Learning', func: this._gotoLearning},
  144. ];
  145.  
  146. get helpId() {
  147. return this._helpId;
  148. }
  149.  
  150. set helpId(val) {
  151. this._helpId = val;
  152. }
  153.  
  154. _gotoNavLink(item) {
  155. clickElement(document, [`#global-nav a[href*="/${item}"`]);
  156. }
  157.  
  158. _gotoNavButton(item) {
  159. const buttons = Array.from(document.querySelectorAll('#global-nav button'));
  160. const button = buttons.find(el => el.textContent.includes(item));
  161. if (button) {
  162. button.click();
  163. }
  164. }
  165.  
  166. _help() {
  167. const help = document.querySelector(`#${this.helpId}`);
  168. help.showModal();
  169. help.focus();
  170. }
  171.  
  172. _gotoSearch() {
  173. clickElement(document, ['#global-nav-search button']);
  174. }
  175.  
  176. _goHome() {
  177. this._gotoNavLink('feed');
  178. }
  179.  
  180. _gotoMyNetwork() {
  181. this._gotoNavLink('mynetwork');
  182. }
  183.  
  184. _gotoJobs() {
  185. this._gotoNavLink('jobs');
  186. }
  187.  
  188. _gotoMessaging() {
  189. this._gotoNavLink('messaging');
  190. }
  191.  
  192. _gotoNotifications() {
  193. this._gotoNavLink('notifications');
  194. }
  195.  
  196. _gotoProfile() {
  197. this._gotoNavButton('Me');
  198. }
  199.  
  200. _gotoBusiness() {
  201. this._gotoNavButton('Business');
  202. }
  203.  
  204. _gotoLearning() {
  205. this._gotoNavLink('learning');
  206. }
  207.  
  208. }
  209.  
  210. class Feed extends Page {
  211. _pathname = '/feed/';
  212. _click_handler_selector = 'main';
  213. _auto_keys = [
  214. {seq: 'X', desc: 'Toggle hiding current post', func: this._togglePost},
  215. {seq: 'j', desc: 'Next post', func: this._nextPost},
  216. {seq: 'J', desc: 'Toggle hiding then next post', func: this._nextPostPlus},
  217. {seq: 'k', desc: 'Previous post', func: this._prevPost},
  218. {seq: 'K', desc: 'Toggle hiding then previous post', func: this._prevPostPlus},
  219. {seq: 'm', desc: 'Show more of the post or comment', func: this._seeMore},
  220. {seq: 'c', desc: 'Show comments', func: this._showComments},
  221. {seq: 'n', desc: 'Next comment', func: this._nextComment},
  222. {seq: 'p', desc: 'Previous comment', func: this._prevComment},
  223. {seq: 'l', desc: 'Load more posts (if the <button>New Posts</button> button is available, load those)', func: this._loadMorePosts},
  224. {seq: 'L', desc: 'Like post or comment', func: this._likePostOrComment},
  225. {seq: 'f', desc: 'Focus on current post or comment (causes browser to change focus)', func: this._focusBrowser},
  226. {seq: 'v p', desc: 'View the post directly', func: this._viewPost},
  227. {seq: 'v r', desc: 'View reactions on current post or comment', func: this._viewReactions},
  228. {seq: '=', desc: 'Open the (⋯) menu', func: this._openMeatballMenu},
  229. ];
  230.  
  231. _currentPostElement = null;
  232. _currentCommentElement = null;
  233.  
  234. _clickHandler(evt) {
  235. const post = evt.target.closest('div[data-id]');
  236. if (post) {
  237. this._post = post;
  238. }
  239. }
  240.  
  241. get _post() {
  242. return this._currentPostElement;
  243. }
  244.  
  245. set _post(val) {
  246. if (val === this._currentPostElement) {
  247. return;
  248. }
  249. if (this._currentPostElement) {
  250. this._currentPostElement.classList.remove('tom');
  251. }
  252. this._currentPostElement = val;
  253. this._comment = null;
  254. if (val) {
  255. val.classList.add('tom');
  256. this._scrollToCurrentPost();
  257. }
  258. }
  259.  
  260. get _comment() {
  261. return this._currentCommentElement;
  262. }
  263.  
  264. set _comment(val) {
  265. if (this._currentCommentElement) {
  266. this._currentCommentElement.classList.remove('dick');
  267. }
  268. this._currentCommentElement = val;
  269. if (val) {
  270. val.classList.add('dick');
  271. this._scrollToCurrentComment();
  272. }
  273. }
  274.  
  275. _getPosts() {
  276. return Array.from(document.querySelectorAll('main div[data-id]'));
  277. }
  278.  
  279. _getComments() {
  280. if (this._post) {
  281. return Array.from(this._post.querySelectorAll('article.comments-comment-item'));
  282. } else {
  283. return [];
  284. }
  285. }
  286.  
  287. _scrollToCurrentPost() {
  288. this._post.style.scrollMarginTop = navBarHeightCss;
  289. this._post.scrollIntoView();
  290. }
  291.  
  292. _scrollToCurrentComment() {
  293. const rect = this._comment.getBoundingClientRect();
  294. this._comment.style.scrollMarginTop = navBarHeightCss;
  295. this._comment.style.scrollMarginBottom = '3em';
  296. // If both scrolling happens, that means the comment is too long
  297. // to fit on the page, so the top is preferred.
  298. if (rect.bottom > document.documentElement.clientHeight) {
  299. this._comment.scrollIntoView(false);
  300. }
  301. if (rect.top < navBarHeightPixels) {
  302. this._comment.scrollIntoView();
  303. }
  304. }
  305.  
  306. _scrollBy(n) {
  307. const posts = this._getPosts();
  308. if (posts.length) {
  309. let idx = posts.indexOf(this._post);
  310. let post = null;
  311. // Some posts are hidden (ads, suggestions). Skip over thoses.
  312. do {
  313. idx = Math.max(Math.min(idx + n, posts.length - 1), 0);
  314. post = posts[idx];
  315. } while (!post.clientHeight);
  316. this._post = post;
  317. }
  318. }
  319.  
  320. _scrollCommentsBy(n) {
  321. const comments = this._getComments();
  322. if (comments.length) {
  323. let idx = comments.indexOf(this._comment);
  324. idx = Math.min(idx + n, comments.length - 1);
  325. if (idx < 0) {
  326. // focus back to post
  327. this._comment = null;
  328. this._post = this._post;
  329. } else {
  330. this._comment = comments[idx];
  331. }
  332. }
  333. }
  334.  
  335. _nextPost() {
  336. this._scrollBy(1);
  337. }
  338.  
  339. _nextPostPlus() {
  340. this._togglePost();
  341. this._nextPost();
  342. }
  343.  
  344. _prevPost() {
  345. this._scrollBy(-1);
  346. }
  347.  
  348. _prevPostPlus() {
  349. this._togglePost();
  350. this._prevPost();
  351. }
  352.  
  353. _nextComment() {
  354. this._scrollCommentsBy(1);
  355. }
  356.  
  357. _prevComment() {
  358. this._scrollCommentsBy(-1);
  359. }
  360.  
  361. _togglePost() {
  362. clickElement(this._post, ['button[aria-label^="Dismiss post"]', 'button[aria-label^="Undo and show"]']);
  363. }
  364.  
  365. _showComments() {
  366. clickElement(this._post, ['button[aria-label*="comment"]']);
  367. }
  368.  
  369. _seeMore() {
  370. const el = this._comment ? this._comment : this._post;
  371. clickElement(el, ['button[aria-label^="see more"]']);
  372. }
  373.  
  374. _likePostOrComment() {
  375. const el = this._comment ? this._comment : this._post;
  376. clickElement(el, ['button[aria-label^="Open reactions menu"]']);
  377. }
  378.  
  379. _loadMorePosts() {
  380. const posts = this._getPosts();
  381. if (clickElement(posts[0], ['div.feed-new-update-pill button'])) {
  382. this._post = posts[0];
  383. } else {
  384. clickElement(document, ['main button.scaffold-finite-scroll__load-button']);
  385. }
  386. this._scrollToCurrentPost();
  387. }
  388.  
  389. _openMeatballMenu() {
  390. if (this._comment) {
  391. // XXX In this case, the aria-label is on the svg element, not
  392. // the button, so use the parentElement.
  393. const button = this._comment.querySelector('[aria-label^="Open options"]').parentElement;
  394. button.click();
  395. } else if (this._post) {
  396. // Yeah, I don't get it. This one isn't the button either,
  397. // but the click works.
  398. clickElement(this._post, ['[aria-label^="Open control menu"]']);
  399. }
  400. }
  401.  
  402. _focusBrowser() {
  403. const el = this._comment ? this._comment : this._post;
  404. if (el) {
  405. const tabIndex = el.getAttribute('tabindex');
  406. el.setAttribute('tabindex', 0);
  407. el.focus();
  408. if (tabIndex) {
  409. el.setAttribute('tabindex', tabIndex);
  410. } else {
  411. el.removeAttribute('tabindex');
  412. }
  413. }
  414. }
  415.  
  416. _viewPost() {
  417. if (this._post) {
  418. const urn = this._post.dataset.id;
  419. const id = `lt-${urn.replaceAll(':', '-')}`;
  420. let a = this._post.querySelector(`#${id}`);
  421. console.debug('queried a', a);
  422. if (!a) {
  423. a = document.createElement('a');
  424. a.href = `/feed/update/${urn}/`;
  425. a.id = id;
  426. this._post.append(a);
  427. }
  428. a.click();
  429. }
  430. }
  431.  
  432. _viewReactions() {
  433. // Bah! The queries are annoyingly different.
  434. if (this._comment) {
  435. clickElement(this._comment, ['button.comments-comment-social-bar__reactions-count']);
  436. } else if (this._post) {
  437. clickElement(this._post, ['button.social-details-social-counts__count-value']);
  438. }
  439. }
  440.  
  441. }
  442.  
  443. class Jobs extends Page {
  444. _pathname = '/jobs/';
  445. }
  446.  
  447. class JobsCollections extends Page {
  448. _pathname = '/jobs/collections/';
  449. }
  450.  
  451. class Notifications extends Page {
  452. _pathname = '/notifications/';
  453. _auto_keys = [
  454. {seq: 'j', desc: 'Next notification', func: this._nextNotification},
  455. {seq: 'k', desc: 'Previous notification', func: this._prevNotification},
  456. {seq: 'a', desc: 'Activate the notification (click on it)', func: this._activateNotification},
  457. {seq: 'X', desc: 'Toggle current notification deletion', func: this._deleteNotification},
  458. {seq: '=', desc: 'Open the (⋯) menu', func: this._openMeatballMenu},
  459. ];
  460.  
  461. // Ugh. When notifications are deleted, the entire element, and
  462. // parent elements, are deleted and replaced by new elements. So
  463. // the only way to track them is by array position.
  464. _currentNotificationIndex = -1;
  465.  
  466. get _notification() {
  467. if (this._currentNotificationIndex >= 0) {
  468. return this._getNotifications()[this._currentNotificationIndex];
  469. } else {
  470. return null;
  471. }
  472. }
  473.  
  474. set _notification(val) {
  475. if (this._notification) {
  476. this._notification.classList.remove('tom');
  477. }
  478. if (val) {
  479. const notifications = this._getNotifications();
  480. this._currentNotificationIndex = notifications.indexOf(val);
  481. val.classList.add('tom');
  482. this._scrollToCurrentNotification();
  483. }
  484. }
  485.  
  486. _getNotifications() {
  487. return Array.from(document.querySelectorAll('main section div.nt-card-list article'));
  488. }
  489.  
  490. _scrollToCurrentNotification() {
  491. const rect = this._notification.getBoundingClientRect();
  492. this._notification.style.scrollMarginTop = navBarHeightCss;
  493. this._notification.style.scrollMarginBottom = '3em';
  494. if (rect.bottom > document.documentElement.clientHeight) {
  495. this._notification.scrollIntoView(false);
  496. }
  497. if (rect.top < navBarHeightPixels) {
  498. this._notification.scrollIntoView();
  499. }
  500. }
  501.  
  502. _scrollBy(n) {
  503. const notifications = this._getNotifications();
  504. if (notifications.length) {
  505. const idx = Math.max(Math.min(this._currentNotificationIndex + n, notifications.length - 1), 0);
  506. this._notification = notifications[idx];
  507. }
  508. }
  509.  
  510. _nextNotification() {
  511. this._scrollBy(1);
  512. }
  513.  
  514. _prevNotification() {
  515. this._scrollBy(-1);
  516. }
  517.  
  518. _openMeatballMenu() {
  519. clickElement(this._notification, ['button[aria-label^="Settings menu"]']);
  520. }
  521.  
  522. _activateNotification() {
  523. if (this._notification) {
  524. // Every notification is different.
  525. // It may be that notifications are settling on 'a.nt-card__headline'.
  526. function matchesKnownText(el) {
  527. if (el.innerText === 'Apply early') return true;
  528. return false;
  529. }
  530.  
  531. // Debugging code.
  532. if (this._notification.querySelectorAll('a.nt-card__headline').length === 1 && this._notification.querySelector('button.message-anywhere-button')) {
  533. console.debug(this._notification);
  534. alert('Yes, can be simplified');
  535. }
  536.  
  537. if (!clickElement(this._notification, ['button.message-anywhere-button'])) {
  538. const buttons = Array.from(this._notification.querySelectorAll('button'));
  539. const button = buttons.find(matchesKnownText);
  540. if (button) {
  541. button.click();
  542. } else {
  543. const links = this._notification.querySelectorAll('a.nt-card__headline');
  544. if (links.length === 1) {
  545. links[0].click();
  546. } else {
  547. console.debug(this._notification);
  548. for (const el of this._notification.querySelectorAll('*')) {
  549. console.debug(el);
  550. }
  551. const msg = [
  552. 'You tried to activate an unsupported notification',
  553. 'element. Please file a bug. If you are comfortable',
  554. 'with using the browser\'s Developer Tools (often the',
  555. 'F12 key), consider sharing the information just logged',
  556. 'in the console / debug view.',
  557. ];
  558. alert(msg.join(' '));
  559. }
  560. }
  561. }
  562. }
  563. }
  564.  
  565. _deleteNotification() {
  566. if (this._notification) {
  567. // Hah. Unlike in other places, these buttons already exist,
  568. // just hidden under the menu.
  569. const buttons = Array.from(this._notification.querySelectorAll('button'));
  570. const button = buttons.find(el => el.textContent.includes('Delete this notification'));
  571. if (button) {
  572. button.click();
  573. } else {
  574. clickElement(this._notification, ['button[aria-label^="Undo notification deletion"]']);
  575. }
  576. }
  577. }
  578.  
  579. }
  580.  
  581. class Pages {
  582. _global = null;
  583. _page = null;
  584. _pages = new Map();
  585.  
  586. _lastInputElement = null;
  587.  
  588. constructor() {
  589. this._id = crypto.randomUUID();
  590. this._installNavStyle();
  591. this._initializeHelpMenu();
  592. document.addEventListener('focus', this._onFocus.bind(this), true);
  593. document.addEventListener('href', this._onHref.bind(this), true);
  594. }
  595.  
  596. _setInputFocus(state) {
  597. const pages = Array.from(this._pages.values());
  598. pages.push(this._global);
  599. for (const page of pages) {
  600. if (page) {
  601. page.keyboard.setContext('inputFocus', state);
  602. }
  603. }
  604. }
  605.  
  606. _onFocus(evt) {
  607. if (this._lastInputElement && evt.target !== this._lastInputElement) {
  608. this._lastInputElement = null
  609. this._setInputFocus(false);
  610. }
  611. if (isInput(evt.target)) {
  612. this._setInputFocus(true);
  613. this._lastInputElement = evt.target;
  614. }
  615. }
  616.  
  617. _onHref(evt) {
  618. this.activate(evt.detail.url.pathname);
  619. }
  620.  
  621. _installNavStyle() {
  622. const style = document.createElement('style');
  623. style.textContent += '.tom { border-color: orange !important; border-style: solid !important; border-width: medium !important; }';
  624. style.textContent += '.dick { border-color: red !important; border-style: solid !important; border-width: thin !important; }';
  625. document.head.append(style);
  626. }
  627.  
  628. _initializeHelpMenu() {
  629. this._helpId = `help-${this._id}`;
  630. const style = document.createElement('style');
  631. style.textContent += `#${this._helpId} kbd {font-size: 0.85em; padding: 0.07em; border-width: 1px; border-style: solid; }`;
  632. style.textContent += `#${this._helpId} th { padding-top: 1em; text-align: left; }`;
  633. style.textContent += `#${this._helpId} td:first-child { white-space: nowrap; text-align: right; padding-right: 0.5em; }`;
  634. style.textContent += `#${this._helpId} button { border-width: 1px; border-style: solid; border-radius: 0.25em; }`;
  635. document.head.prepend(style);
  636. const dialog = document.createElement('dialog');
  637. dialog.id = this._helpId
  638. dialog.innerHTML = '<table><caption>' +
  639. '<span style="float: left">Keyboard shortcuts</span>' +
  640. '<span style="float: right">Hit <kbd>ESC</kbd> to close</span>' +
  641. '</caption><tbody></tbody></table>';
  642. document.body.prepend(dialog);
  643. }
  644.  
  645. // ThisPage -> This Page
  646. _parseHeader(text) {
  647. return text.replace(/([A-Z])/g, ' $1').trim();
  648. }
  649.  
  650. // 'a b' -> '<kbd>a</kbd> then <kbd>b</kbd>'
  651. _parseSeq(seq) {
  652. const letters = seq.split(' ').map(w => `<kbd>${w}</kbd>`);
  653. const s = letters.join(' then ');
  654. return s;
  655. }
  656.  
  657. _addHelp(page) {
  658. const help = document.querySelector(`#${this._helpId} tbody`);
  659. const section = this._parseHeader(page.helpHeader);
  660. let s = `<tr><th></th><th>${section}</th></tr>`;
  661. for (const {seq, desc} of page.helpContent) {
  662. const keys = this._parseSeq(seq);
  663. s += `<tr><td>${keys}:</td><td>${desc}</td></tr>`;
  664. }
  665. // Don't include works in progress that have no keys yet.
  666. if (page.helpContent.length) {
  667. help.innerHTML += s;
  668. }
  669. }
  670.  
  671. register(page) {
  672. page.start();
  673. this._addHelp(page);
  674. if (page.pathname === null) {
  675. page.helpId = this._helpId
  676. this._global = page;
  677. this._global.activate();
  678. } else {
  679. this._pages.set(page.pathname, page);
  680. }
  681. }
  682.  
  683. _findPage(pathname) {
  684. const pathnames = Array.from(this._pages.keys());
  685. const candidates = pathnames.filter(p => pathname.startsWith(p));
  686. const candidate = candidates.reduce((a, b) => {
  687. return a.length > b.length ? a : b;
  688. }, '');
  689. return this._pages.get(pathname) || null;
  690. }
  691.  
  692. activate(pathname) {
  693. if (this._page) {
  694. this._page.deactivate();
  695. }
  696. const page = this._findPage(pathname);
  697. this._page = page;
  698. if (page) {
  699. page.activate();
  700. }
  701. }
  702. }
  703.  
  704. const pages = new Pages();
  705. pages.register(new Global());
  706. pages.register(new Feed());
  707. pages.register(new Jobs());
  708. pages.register(new JobsCollections());
  709. pages.register(new Notifications());
  710. pages.activate(window.location.pathname);
  711.  
  712. function isInput(element) {
  713. let tagName = '';
  714. if ('tagName' in element) {
  715. tagName = element.tagName.toLowerCase();
  716. }
  717. return (element.isContentEditable || ['input', 'textarea'].includes(tagName));
  718. }
  719.  
  720. // Run querySelector to get an element, then click it.
  721. function clickElement(base, selectorArray) {
  722. if (base) {
  723. for (const selector of selectorArray) {
  724. const el = base.querySelector(selector);
  725. if (el) {
  726. el.click();
  727. return true;
  728. }
  729. }
  730. }
  731. return false;
  732. }
  733.  
  734. let navBarHeightPixels = 0;
  735. let navBarHeightCss = '0';
  736. VM.observe(document.body, () => {
  737. const navbar = document.querySelector('#global-nav');
  738.  
  739. if (navbar) {
  740. navBarHeightPixels = navbar.clientHeight + 4;
  741. navBarHeightCss = `${navBarHeightPixels}px`;
  742.  
  743. return true;
  744. }
  745. });
  746.  
  747. let oldUrl = new URL(window.location);
  748. VM.observe(document.body, () => {
  749. const newUrl = new URL(window.location);
  750. if (oldUrl.href !== newUrl.href) {
  751. const evt = new CustomEvent('href', {detail: {url: newUrl}})
  752. oldUrl = newUrl;
  753. document.dispatchEvent(evt);
  754. }
  755. });
  756.  
  757. })();