- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
理论上,如果我设置 run_at,我用来选择页面上元素的 .js 应该在页面完全加载后触发。但似乎并非如此。
在我的 content.js 中,我有
"content_scripts": [
{
"js": ["extensions/jquery-2.2.2.min.js", "content.js"],
"run_at": "document_idle"
}
]
在我的 content.js 中,我只有:
alert("alert");
console.log("hello");
var fullname2 = $('#topcard').find('.profile-info').find('h1').text();
console.log(fullname2);
警报和第一个 console.log 都有效。第二个控制台日志不记录任何内容,因为选择器未能选择任何内容。我知道这一点,因为如果我为 content.js 代码块添加一个 setTimeout,并等待 2 秒,第二个控制台日志会正确显示 fullname2。
知道为什么 content.js 显然在页面完全加载之前触发,以至于选择器找不到任何东西吗?
最佳答案
run_at: "document_idle"
保证脚本在 DOMContentLoaded
事件之后执行,该事件表明所有 static DOM 内容都已解析且可用。
如果一个元素尚不存在,这意味着稍后将通过页面自己的代码动态添加它。
显然,Chrome 无法预测页面何时(如果有的话!)处于“完成”状态。您需要设置自己的标准(例如“当 #topcard
出现时”)。
话虽如此,您的策略应该如下:
检查元素是否已经存在。如果是,处理它,你就完成了。
设置 listener for changes in the document 。这是通过 MutationObserver
完成的,为了简单起见,我强烈推荐 mutation-summary
library。
关于javascript - Chrome 扩展 : the js script fires too early despite run_at: document_idle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37310596/
我有几个工作是通过 after_create 设置的一个物体的钩子(Hook)。在我的测试期间,我一直在保存对象,因此大约 5 分钟后触发一项作业,并在 10 分钟后触发一项作业(使用模型的两个属性,
在正在加载的页面上执行任何其他代码之前,我需要运行一些 JavaScript 代码。 使用这样的内容脚本效果很好: { "matches": ["http://*/*", "https://*/*
编辑:我的 Chrome 浏览器出现问题并与我的脚本发生冲突,完全重新安装消除了问题源。如果我碰巧发现是什么原因造成的,我会把它包括在这里。 EDIT2:只是为了让在 2017 年阅读本文的任何人都知
我在这里有点不知所措。 例如,我只是想在用户定义的时间延迟 checkin 。我想让它尽可能简单。 编辑 : 解决了,见下。 class MyController @time.minutes.fro
我已经为 delayed_job 安装了 collectiveidea fork git://github.com/collectiveidea/delayed_job.git 但无法让它接受 :ru
作为测试,我正在创建一个 Chrome 扩展,它隐藏了 Facebook 右侧的“新闻”部分和右侧广告。还将背景颜色更改为黑色。 我现在的问题是,当您加载 Facebook 时,新闻和白色背景会显示一
理论上,如果我设置 run_at,我用来选择页面上元素的 .js 应该在页面完全加载后触发。但似乎并非如此。 在我的 content.js 中,我有 "content_scripts": [
当我使用内容脚本将一些 javascript 注入(inject) HTML 页面时,javascript 的执行顺序有问题: 这是我用来测试的 HTML 页面,test.html: Test
我是一名优秀的程序员,十分优秀!