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

时间:2010-10-25 22:30:55

标签: google-chrome google-chrome-extension

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

参见Page action by content三明治示例(files),这基本上就是我在做的事情。如果您加载了扩展程序,您会看到只有在页面顶部显示“三明治”一词时才能使用扩展程序。

2 个答案:

答案 0 :(得分:18)

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

"run_at": "document_end"

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

答案 1 :(得分:5)

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

<强>的manifest.json

"content_scripts": [
        "js": ["content.js"],
        "run_at": "document_start"
    }
]

<强> 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);
相关问题