Chrome扩展程序仅在刷新时运行,而不是在页面导航上运行

时间:2016-09-21 23:05:32

标签: javascript jquery google-chrome google-chrome-extension

我有一个chrome扩展,我想操纵GitHub上的一些DOM。当我刷新任何给定页面时,所有工作都按预期工作,但如果我通常导航到该页面,则脚本不会被执行。

的manifest.json

{
    "manifest_version": 2,

    "name": "Name",
    "description": "Detailed description",
    "version": "1.3.5",
    "content_scripts": [{
        "matches": ["https://github.com/*/*/issues/*"],
        "js": ["jquery-2.1.0.min.js", "index.js"]
    }],
    "browser_action": {
        "default_icon": "icon.png"
    },
    "permissions": [
        "activeTab",
        "https://ajax.googleapis.com/"
    ]
}

index.js

$(document).ready(function() {
    // DOM manipulating code
    // removed for brevity
});

1 个答案:

答案 0 :(得分:1)

当您从另一个页面导航到GitHub页面时,页面中的容器会更新,而不是整个页面。

所以,根据我的经验...如果你从repo的主页面开始并切换到问题页面,扩展程序并不总是注意到这一变化。因此,我建议将清单matches更改为所有github,而不仅仅是问题:

{
    "manifest_version": 2,
    "name": "Name",
    "description": "Detailed description",
    "version": "1.3.5",
    "content_scripts": [{
        "matches": ["https://github.com/*"],
        "js": ["jquery-2.1.0.min.js", "index.js"]
    }],
    "browser_action": {
        "default_icon": "icon.png"
    },
    "permissions": [
        "activeTab",
        "https://ajax.googleapis.com/"
    ]
}

如果这不起作用,那么在您的代码中,监视文档以查找“pjax:end”事件:

document.addEventListener("pjax:end", function() {
    // run code/call function
});
相关问题