Chrome扩展CORB:如何对共享DOM中的更新做出反应

问题描述 投票:2回答:1

[尝试构建chrome扩展内容脚本,以向GitHub问题页面添加额外的有用导航。通过正常网页进行交互(最终用户单击反应表情符号)时,我注入的元素丢失了。

我能够解决它的唯一方法是设置一个间隔,该间隔会不断删除并将计数器元素注入页面中。

必须有一种比这更优雅的方法,它可以对DOM更改做出响应,以便我可以删除并重新注入元素(而不是一直敲门)?

addReactionsNav.js

const URL =
  window.location.origin + window.location.pathname + window.location.search
const header = document.querySelector('#partial-discussion-sidebar')
header.style = `position: relative;height: 100%;`
let wrapper = getWrapper()

// // The isolated world made it difficult to detect DOM changes in the shared DOM
// // So this monkey-hack to make it refresh when ..
// setInterval(() => {
//   wrapper.remove()
//   wrapper = getWrapper()
//   addReactionNav()
// }, 1000)

// Select the node that will be observed for mutations
const targetNode = document.querySelector('body')

// Options for the observer (which mutations to observe)
const config = { attributes: true, childList: true, subtree: true }

// Create an observer instance linked to the callback function
const observer = new MutationObserver(() => addReactionNav())

// Start observing the target node for configured mutations
observer.observe(targetNode, config)

function getWrapper() {
  const header = document.querySelector('#partial-discussion-sidebar')
  const wrapper = header.appendChild(document.createElement('div'))
  wrapper.style = `
      position:sticky;
      position: -webkit-sticky;
      top:10px;`
  return wrapper
}

function addReactionNav() {
  const title = document.createElement('div')
  title.style = `font-weight: bold`
  title.appendChild(document.createTextNode('Reactions'))
  wrapper.appendChild(title)

  // Grabbing all reactions Reactions �� �� �� �� ❤️ �� �� ��
  const reactionsNodes = document.querySelectorAll(`
    [alias="+1"].mr-1,
    [alias="rocket"].mr-1,
    [alias="tada"].mr-1,
    [alias="heart"].mr-1,
    [alias="smile"].mr-1,
    [alias="thinking_face"].mr-1,
    [alias="-1"].mr-1,
    [alias="eyes"].mr-1
  `)

  const reactionsNodesParents = [
    ...new Set(
      Array.from(reactionsNodes).map(node => node.parentElement.parentElement)
    ),
  ]

  reactionsNodesParents.forEach(node => {
    const a = document.createElement('a')
    const linkText = document.createTextNode('\n' + node.innerText)
    a.appendChild(linkText)
    a.title = node.innerText

    let id = null
    while (id == null || node != null) {
      if (node.tagName === 'A' && node.name) {
        id = node.name
        break
      }

      if (node.id) {
        id = node.id
        break
      }

      node = node.parentNode
    }
    const postURL = URL + '#' + id
    a.href = postURL
    a.style = `display:block;`

    wrapper.appendChild(a)
  })
}

manifest.json

{
  "manifest_version": 2,
  "name": "Github Issue Reactions",
  "version": "1.0",
  "description": "List a link of reactions on a github issue page",
  "permissions": ["https://www.github.com/", "http://www.github.com/"],
  "content_scripts": [
    {
      "matches": ["*://*.github.com/*/issues/*"],
      "js": ["addReactionsNav.js"],
      "run_at": "document_end"
    }
  ]
}

发现关于“孤立世界”的简短提及

https://youtu.be/laLudeUmXHM?t=79

更新

我现在认为“错误”是由于CORB引起的-这是针对Spectre的安全措施。

跨域读取阻止(CORB)阻止了MIME类型为application / json的跨域响应https://api.github.com/_private/browser/stats。有关更多详细信息,请参见https://www.chromestatus.com/feature/5629709824032768

Google在他们的演讲Lessons from Spectre and Meltdown, and how the whole web is getting safer (Google I/O '18)中解释了更多有关此问题的信息>

34:00处提到的示例此后似乎已被CORB阻止。

[尝试构建chrome扩展内容脚本,以向GitHub问题页面添加额外的有用导航。通过正常网页进行交互时(最终用户单击反应表情符号)-我的...

javascript google-chrome dom google-chrome-extension
1个回答
0
投票

如果当前选项卡已更新,则可以将消息传递回您的内容脚本以重新注入您的内容。

© www.soinside.com 2019 - 2024. All rights reserved.