在Chrome控制台中使用javascript制作Google应用

时间:2016-02-11 14:32:29

标签: javascript google-chrome web-applications

var inputs = document.getElementsByClassName('uiButtonText'); for(var i=0; i<inputs.length;i++) { inputs[i].click(); }

我有这个Javascript代码。我想将其转换为扩展程序(Chrome webapp供个人使用)。这是每次我在Chrome浏览器中点击它。我想执行这个javascript。 我怎样才能做到这一点 所以我可以避免经常在控制台中输入此代码。 的manifest.json

  1{                                                                           
  2           "manifest_version": 2,
  3 
  4                     "name": "blah",
  5                       "description": "blah blah",
  6                         "version": "1.0",
  7 
  8                           "browser_action": {
  9                                      "default_icon": "icon.png",
 10                                     "default_popup": "popup.html"
 11                                                   },
 12                             "permissions": [
 13                                        "activeTab"
 14                                           ],
 15                    "web_accessible_resources": [
 16                                          "popup.js"
 17                                                 ]
 18 }

popup.html

<!doctype html>
<html>
  <head>
    <title>Congoroo</title>
    <script src="popup.js"></script>
  </head>
  <body>
    <h1>HaAHAHAH</h1>
    <button id="checkPage">Check this</button>
  </body>
</html>

popup.js

var inputs = document.getElementsByClassName('uiButtonText'); for(var i=0; i<inputs.length;i++) { inputs[i].click(); }
popup.js中的

代码在chrome的控制台中完美运行。 但是在这个扩展中我没有发生任何事情!

1 个答案:

答案 0 :(得分:0)

的manifest.json

{
      "name": "InviteThemAll",
            "version": "0.0.1",
              "manifest_version": 2,
                "description": "Invite all Friends in Fb to Fanpage",
              "homepage_url": "http://felixjosemon.github.io/",
                "background": {
                        "scripts": [
                              "background.js"
                                  ],
                        "persistent": true
                              },
                  "browser_action": {
                          "default_title": "FB!"
                            },
                    "permissions": [
                        "https://*/*",
                    "http://*/*",
                        "tabs"
                          ]
}

popup.js

(function() {

 var inputs = document.getElementsByClassName('uiButtonText'); for(var i=0; i<inputs.length;i++) { inputs[i].click(); } })();

background.js

// this is the background code...

// listen for our browerAction to be clicked
chrome.browserAction.onClicked.addListener(function (tab) {
            // for the current tab, inject the "popup.js" file & execute it
            chrome.tabs.executeScript(tab.ib, {
                        file: 'popup.js'


    });
        });

经过多次尝试后,我创建了background.js,将popup.js注入到当前标签中,这完全符合我的需要。 使用此功能,您可以向所有Fb朋友发送邀请,以便喜欢您的粉丝页面。 https://github.com/Felixjosemon/InviteThemAll