简体   繁体   English

未设置window.innerWidth

[英]window.innerWidth is not being set

I'm currently porting https://github.com/brunolemos/simplified-twitter to Safari. 我目前正在将https://github.com/brunolemos/simplified-twitter移植到Safari。

I'm injecting this content script into Twitter: 我将此内容脚本注入Twitter:

function update(win = window) {
  window.innerWidth = Math.min(win.outerWidth, 800)
  document.body.style.paddingLeft = win.innerWidth >= 800 ? '176px' : null
}

function triggerUpdate() {
  setTimeout(() => window.dispatchEvent(new Event('resize')), 1)
}

window.onresize = e => update(e.target || window)
window.onload = triggerUpdate
document.addEventListener('visibilitychange', triggerUpdate)

triggerUpdate()

The function update() works, since when I check document.body.style.paddingLeft in the console, it says 176px. 函数update()起作用,因为当我在控制台中检查document.body.style.paddingLeft时,它显示为176px。

But when I check window.innerWidth it tells me 1680px instead of 800px. 但是当我检查window.innerWidth它告诉我1680px而不是800px。

What am I doing wrong? 我究竟做错了什么?

Try this. 尝试这个。

function update() {
  document.body.style.paddingLeft = window.outerWidth >= 800 ? '176px' : null
}

function triggerUpdate() {
  setTimeout(() => window.dispatchEvent(new Event('resize')), 1)
}

window.onresize = update
window.onload = triggerUpdate
document.addEventListener('visibilitychange', triggerUpdate)

triggerUpdate()

This was kind of tricky. 这有点棘手。 Apparently when I'm modifying window, I'm not actually modifying the window of twitter.com 显然,当我修改窗口时,实际上并没有修改twitter.com的窗口

To do that, I would need to inject a tag into twitter.com. 为此,我需要将标签注入twitter.com。

To do this, I can create a second script (call it inject.js), add it to the Content Scripts Array in the Info.plist and write this in it: 为此,我可以创建第二个脚本(将其称为inject.js),将其添加到Info.plist中的Content Scripts Array中,并将其编写为:

const script = document.createElement('script')
script.src = safari.extension.baseURI + "script.js"
document.documentElement.appendChild(script)

This will create a new script element, assign the URI to the injected script to its src attribute and append it to the document. 这将创建一个新的脚本元素,将注入脚本的URI分配给其src属性,并将其附加到文档中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM