Rllmuk Put Edit Link Under Your Own Posts

Replace the Quote link under your own posts with an Edit link

目前為 2021-03-31 提交的版本,檢視 最新版本

// ==UserScript==
// @name        Rllmuk Put Edit Link Under Your Own Posts
// @description Replace the Quote link under your own posts with an Edit link
// @namespace   https://github.com/insin/greasemonkey/
// @version     1
// @match       https://rllmukforum.com/index.php?/topic/*
// @match       https://www.rllmukforum.com/index.php?/topic/*
// ==/UserScript==

void function TopicPage() {
  let $loggedInUserLink = document.querySelector('#elUserNav a.ipsUserPhoto')
  if (!$loggedInUserLink) {
    return
  }

  let currentUserId = /profile\/(\d+)/.exec($loggedInUserLink.href)[1]

  function processPosts(context = document) {
    context.querySelectorAll('article.ipsComment').forEach($post => {
      let $userLink = $post.querySelector('.cAuthorPane_author a')
      let userId = /profile\/(\d+)/.exec($userLink.href)[1]
      console.log({$post, userId, currentUserId})
      if (userId !== currentUserId) {
        return
      }
      let $quoteLink = $post.querySelector('a[data-action="quoteComment"]')
      let $editLink = $post.querySelector('a[data-action="editComment"]')
      $quoteLink.replaceWith($editLink.cloneNode(true))
    })
  }

  // Process initial posts
  processPosts()

  // Watch for posts being replaced when paging
  new MutationObserver(mutations =>
    mutations.forEach(mutation => {
      if (mutation.oldValue == 'true') {
        processPosts()
      }
    })
  ).observe(document.querySelector('div.cTopic'), {
    attributes: true,
    attributeFilter: ['animating'],
    attributeOldValue: true,
  })

  // Watch for new posts being loaded into the current page
  new MutationObserver(mutations => {
    mutations.forEach(mutation =>
      mutation.addedNodes.forEach(processPosts)
    )
  }).observe(document.querySelector('#elPostFeed > form'), {
    childList: true,
  })
}()