首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TamperMonkey用户脚本不会触发DOMContentLoaded事件

TamperMonkey用户脚本不会触发DOMContentLoaded事件
EN

Stack Overflow用户
提问于 2016-06-13 19:57:01
回答 1查看 3.5K关注 0票数 3

这是一个TamperMonkey用户脚本。为什么不弹出“你好”?我在Ubuntu上运行谷歌Chrome。

代码语言:javascript
复制
// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        http://*/*
// @match        https://*/*
// @grant        none
// ==/UserScript==

window.addEventListener("DOMContentLoaded", function(event) {
    alert("HELLO");
  });

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-13 20:08:49

用这个:

代码语言:javascript
复制
// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        http://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    if (document.readyState == "complete" || document.readyState == "loaded" || document.readyState == "interactive") {
        alert("Already Loaded");
    } else {
        document.addEventListener("DOMContentLoaded", function(event) {
            alert("Just Loaded");
        });
    }
})();

How to detect if DOMContentLoaded was fired借来的。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37798132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档