Google Chrome扩展程序 - 等待页面加载

yxk*_*yxk 16 google-chrome google-chrome-extension

在我的Google Chrome扩展程序中,我有一个内容脚本(content.js)和一个背景页面(background.html).我有context.js检查页面上显示的关键字.但是,我想等到页面完全加载,直到我搜索页面,因为关键字可能出现在页面的底部.

通过内容三明治示例(文件)查看页面操作,这基本上就是我正在做的事情.如果您加载扩展程序,您将看到扩展程序仅在页面顶部显示"三明治"一词时才有效.

小智 19

尝试将其添加到manifest.json文件的"content_scripts"部分.

"run_at": "document_end"
Run Code Online (Sandbox Code Playgroud)

http://code.google.com/chrome/extensions/content_scripts.html


dhi*_*ilt 7

作为清单的替代方案"run_at": "document_end",可以应用标准的JavaScript事件处理方法。例如,可以使用DOMContentLoaded事件来运行需要加载的DOM的逻辑。

manifest.json

"content_scripts": [
        "js": ["content.js"],
        "run_at": "document_start"
    }
]
Run Code Online (Sandbox Code Playgroud)

content.js

console.log('The extension works');
// ... logic that does not need DOM

function run() {
  console.log('The DOM is loaded');
  // ... logic that needs DOM
}

document.addEventListener("DOMContentLoaded", run);
Run Code Online (Sandbox Code Playgroud)

  • 您的意思是“run_at”:“document_end”而不是“run_at”:“document_start”? (2认同)