选中具有特定类的页面上的复选框

时间:2012-11-21 13:29:10

标签: google-chrome google-chrome-extension

所以我已经受够了这一页,如果我想发送私信,我总是要检查一个复选框,当我发送消息时,我希望它始终是私有的。所以我决定尝试制作关于它的谷歌浏览器扩展程序。所以我创建了默认文件manifest.jsonpopup.html,但我无法让它工作。

我做错了什么?

Manifest.json

{
  "name": "xxx",
  "version": "1.0",
  "manifest_version": 2,
  "description": "Automaattinen yksityiskommentointi.",
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  }
}

popup.html

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>XXX</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="script.js"></script>
</head>

<body>
Moi
</body>
</html>

script.js

$(document).ready(function(){
    $('.private-checkbox').prop('checked', true);   
});

这太简单吧?我打赌这只会检查弹出窗口中的复选框,但我想在我打开的标签中选中复选框。

1 个答案:

答案 0 :(得分:0)

对我来说,popup.html中有以下更改

下载jquery.min.js并将其添加到您的包中,如图所示

使用

<script src="jquery.min.js"></script>

而不是

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 
  

仅加载本地脚本和对象资源

脚本和对象资源只能从扩展程序包中加载,而不能从Web中加载。这可确保您的扩展程序仅执行您特别批准的代码,从而防止活动网络攻击者恶意重定向您的资源请求。

更新了Manifest.json

{
  "name": "xxx",
  "version": "1.0",
  "manifest_version": 2,
  "description": "Automaattinen yksityiskommentointi.",
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
"content_scripts": [
    {
      "matches": ["http://www.facebook.com/*"],
      "js": ["jquery.min.js","myscript.js"]
    }
  ]
    }

<强> myscript.js

$(document).ready(function(){
    $('.private-checkbox').prop('checked', true);   
});