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

Nor*_*ldt 6 javascript dom google-chrome google-chrome-extension

尝试构建一个 chrome 扩展内容脚本,为 GitHub 问题页面添加一个额外有用的导航。当通过普通网页(最终用户单击反应表情符号)完成交互时 - 我注入的元素丢失了。

我能够绕过它的唯一方法是设置一个间隔,不断删除和注入我的计数器元素到页面中。

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

我正在尝试优化的扩展可以在这里找到

https://github.com/NorfeldtAbtion/github-issue-reactions-chrome-extension

重要文件目前看起来像这样

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)
  })
}
Run Code Online (Sandbox Code Playgroud)

清单文件

{
  "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"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

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

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

更新

我现在相信“错误”是由 CORB 造成的——这是一种针对 Spectre 的安全措施。

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

Google 在他们的演讲Spectre 和 Meltdown 的教训以及整个网络如何变得更安全(Google I/O '18)中对此进行了更多解释

从那以后34:00提到的示例似乎已被 CORB 阻止。

Vin*_* W. 1

#partial-discussion-sidebar由于当“最终用户在第一篇文章中单击反应表情符号”时GitHub 会替换整个节点,因此您需要在突变观察者响应getWrapper()之前再次执行addReactionNav()此操作,如下所示。

更新:由于#partial-discussion-sidebar除了第一个帖子之外的帖子上的反应更新时,节点不会重新渲染,因此我们还需要响应时间线项目的更新。

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();
addReactionNav();    // Initial display.

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

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

// Create an observer instance linked to the callback function.
const observer = new MutationObserver(mutations => {
  if (!targetNode.contains(wrapper) || mutations.some(mutation => mutation.target.matches('.js-timeline-item'))) {
    wrapper.remove();
    wrapper = getWrapper();
    addReactionNav();
  }
});

// Start observing the target node for configured mutations.
observer.observe(targetNode, config);
Run Code Online (Sandbox Code Playgroud)