Coder Social home page Coder Social logo

Comments (2)

james-tindal avatar james-tindal commented on June 9, 2024

LinkedIn changed the behaviour of the notifications page.
Posts now link to linkedin.com/feed?highlightUpdateUrn=...
That page shows the post at the top followed by the usual feed.

Two possible solutions: Detect highlightedUpdateUrn in the query params and

  • show only the first item in the feed.
  • redirect to the same post at linkedin.com/posts/... (would be a faff to find the right url, and possibly prone to breaking)

from news-feed-eradicator.

james-tindal avatar james-tindal commented on June 9, 2024

Here's a userscript to fix this problem:

Click here
// ==UserScript==
// @name         Fix LinkedIn notifications page
// @description  Fix notification links so they go to the post or activity page instead of the feed.
// @version      2024-01-19
// @match        https://www.linkedin.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=linkedin.com
// ==/UserScript==

(() => {
  'use strict'

  /*  Monkey-patch locationchange event  */

  const oldPushState = history.pushState
  history.pushState = function pushState() {
    const ret = oldPushState.apply(this, arguments)
    window.dispatchEvent(new Event('locationchange'))
    return ret
  }

  const oldReplaceState = history.replaceState
  history.replaceState = function replaceState() {
    const ret = oldReplaceState.apply(this, arguments)
    window.dispatchEvent(new Event('locationchange'))
    return ret
  }

  window.addEventListener('popstate', () =>
    window.dispatchEvent(new Event('locationchange')))


  /*  Listen for notifications page */

  continueIfNotificationsPage()
  window.addEventListener('locationchange', continueIfNotificationsPage)


  async function continueIfNotificationsPage() {
    if (!isNotificationsPage()) return

    const cardList = await waitForElement({ target: "nt-card-list", ancestor: 'application-outlet' })

    changeLinks(cardList)
  }

  function changeLinks(cardList) {
    const initial_notification_elements = cardList.children

    // change link for initial 10 loaded notifications
    for(const el of initial_notification_elements) changeLink(el)

    // change link for any notifications that load later
    const listener = mutation_records => mutation_records
      .forEach(mr => mr.addedNodes.forEach(node => node.nodeName === 'DIV' && changeLink(node)))

    observeMutations({ element: cardList, listener, childList: true })
  }

  function changeLink (notification_element) {
    const link_element = notification_element
      .querySelector('a.nt-card__headline')
    const url = link_element.attributes.href.value
    const newUrl = url
      .replace(/^\/feed\/\?highlightedUpdateUrn=urn%3Ali%3Aactivity%3A(\d{19}).*/, '/feed/update/urn:li:activity:$1')
    link_element.setAttribute('href', newUrl)
    remove_click_events(link_element)
  }

  /* ... */

  function observeMutations({ element, listener, ...options }) {
    const observer = new MutationObserver(listener)
    observer.observe(element, options)
    return observer
  }

  function waitForElement({ ancestor, target }) {
    const ancestorEl = document.getElementsByClassName(ancestor)[0]
    const search = ancestorEl.getElementsByClassName(target)
    if (search.length > 0)
      return Promise.resolve(search[0])

    return new Promise(resolve => {
      const observer = observeMutations({
        listener,
        element: ancestorEl,
        childList: true,
        subtree: true
      })

      function listener(mutationRecords) {
        for (const mutationRecord of mutationRecords) {
          for (const node of mutationRecord.addedNodes)
            if (node?.classList?.contains?.(target)) {
              observer.disconnect()
              resolve(node)
            }
      }}
    })
  }

  // Remove click events overriding the new href
  function remove_click_events(element) {
    element.outerHTML = element.outerHTML
  }

  function isNotificationsPage() {
    const matchNotificationsPage = /^https:\/\/www\.linkedin\.com\/notifications\/?\??.*$/
    return matchNotificationsPage.test(window.location)
  }

})()

from news-feed-eradicator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.