隐藏对话框时运行脚本

时间:2016-01-24 14:58:36

标签: javascript google-chrome-extension

我是Chrome扩展程序开发的新手,我很好奇如何在没有“点击”对话框窗口的情况下使扩展程序运行。现在我的扩展程序只有在我点击图标并显示对话框时才会运行。一旦我点击页面扩展窗口上的其他内容隐藏并且脚本停止执行。这是我的代码。

popup.html

<!doctype html>  
<html>  
    <head><title>activity</title></head>  
<body style="width:300px;">  
    <script src="popup.js"></script> 

</body>
</html>  

popup.js

function injectTheScript() {
        chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        chrome.tabs.executeScript(tabs[0].id, {file: "content_script.js"});
    })
}
 setInterval(function(){   injectTheScript();    },2000);

content_script.js

function clickPlanet() {
    var planets = document.getElementsByClassName("collectbtn"),
        randomplanet = Math.floor(Math.random() * planets.length),
        clickplanet = planets[randomplanet];
    clickplanet.click();  
}  
clickPlanet();

的manifest.json

{
    "manifest_version": 2,
    "name": "Such Activity",  
    "description": "Wow",  
    "version": "1.0",    
    "permissions": ["tabs", "<all_urls>", "background"],  

    "browser_action": { 
        "default_icon": {
            "19": "images/icons/19.png",
            "38": "images/icons/38.png"
        },
        "default_popup": "popup.html"  
    },

"background": {
    "scripts": ["background.js"],
    "persistent": false
    },

    "icons": {
        "16": "images/icons/16.png",
        "19": "images/icons/19.png",
        "38": "images/icons/38.png",
        "64": "images/icons/64.png",
        "128": "images/icons/128.png"
    }
  }

1 个答案:

答案 0 :(得分:0)

如果没有用户交互,则无法显示弹出窗口 它是通过这种方式实现的,以便自己进行扩展以显示弹出窗口,以避免滥用弹出窗口。

另一种方法是,如果你真的需要以编程方式显示某些东西,那就是使用浏览器图标在网页中显示一个iframe,其中包含与弹出窗口相同的html和脚本。

相关问题