Chrome扩展程序jquery选择器

时间:2015-10-08 07:44:25

标签: jquery google-chrome

您好我尝试创建一个chrome扩展程序,当我单击图标按钮时,脚本将运行并找到具有特定ID的元素。 我的清单是这样的:

{
"name": "Highlight some phrases",
"description": "Hightlight some pre defined text from websql database after page loads",
"version": "0.1",
"permissions": [
"tabs","<all_urls>"
],
"background": {
"scripts": ["jquery.js","background.js"]
},
"browser_action": {
    "default_icon": "icon.png"

},

"content_scripts": [
    {
    "matches": [
        "http://*/*",
        "https://*/*"
        ],

    "run_at": "document_start"         // pay attention to this line
    }
], 
"manifest_version":2

}

并且background.js是这样的:

chrome.browserAction.onClicked.addListener(function() {

src = $('#UserName').val();
alert(src);

});

每次单击该图标时,警报始终未定义。 为什么会这样?

3 个答案:

答案 0 :(得分:0)

这可能是因为DOM还没有完成渲染。尝试将您的脚本放在window.onload或jQuery函数中,如下所示:

$(function() {
   chrome.browserAction.onClicked.addListener(function() {

     var src = $('#UserName').val();
     alert(src);

  });
});

另外,请注意不要使用var来声明变量,就像使用src一样,因为您正在污染全局范围。

答案 1 :(得分:0)

这来自文档:

  

在&#34; document_start&#34;的情况下,文件在任何之后注入   来自css的文件,但在构造任何其他DOM之前或任何其他DOM   脚本运行。

您的代码在DOM加载之前工作。未定义是正常的。

尝试改变

中的document_startdocument_end

答案 2 :(得分:0)

奇怪的是,当我将代码更改为implements OnRatingBarChangeListener{ private RatingBar ratingBaR1,ratingBaR2,ratingBaR3,ratingBaR4,ratingBaR5,ratingBaR6,ratingBaR7,ratingBaR8,ratingBaR9; private RadioGroup yesno; Button button; HttpPost httppost; StringBuffer buffer; HttpResponse response; HttpClient httpclient; List<NameValuePair> nameValuePairs; ProgressDialog dialog = null; private EditText name,mob,comments; String rating1,rating2,rating3,rating4,rating5,rating6,rating7,rating8,rating9; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.hello); name=(EditText)findViewById(R.id.cusName); mob=(EditText)findViewById(R.id.cusNumber); ratingBaR1=(RatingBar)findViewById(R.id.ratingBar1); ratingBaR2=(RatingBar)findViewById(R.id.ratingBar2); ratingBaR3=(RatingBar)findViewById(R.id.ratingBar3); ratingBaR4=(RatingBar)findViewById(R.id.ratingBar4); ratingBaR5=(RatingBar)findViewById(R.id.ratingBar5); ratingBaR6=(RatingBar)findViewById(R.id.ratingBar6); ratingBaR7=(RatingBar)findViewById(R.id.ratingBar7); ratingBaR8=(RatingBar)findViewById(R.id.ratingBar8); ratingBaR9=(RatingBar)findViewById(R.id.ratingBar9); // name=(EditText)findViewById(R.id.cusName); // mob=(EditText)findViewById(R.id.cusNumber); // comments=(EditText)findViewById(R.id.comment); ratingBaR1.setOnRatingBarChangeListener((OnRatingBarChangeListener) this); ratingBaR2.setOnRatingBarChangeListener((OnRatingBarChangeListener) this); ratingBaR3.setOnRatingBarChangeListener((OnRatingBarChangeListener) this); ratingBaR4.setOnRatingBarChangeListener((OnRatingBarChangeListener) this); ratingBaR5.setOnRatingBarChangeListener((OnRatingBarChangeListener) this); ratingBaR6.setOnRatingBarChangeListener((OnRatingBarChangeListener) this); ratingBaR7.setOnRatingBarChangeListener((OnRatingBarChangeListener) this); ratingBaR8.setOnRatingBarChangeListener((OnRatingBarChangeListener) this); ratingBaR9.setOnRatingBarChangeListener((OnRatingBarChangeListener) this); } // yesno1.setOnCheckedChangeListener(new OnCheckedChangeListener() { // // public void onCheckedChanged(RadioGroup group, int checkedId) { // // TODO Auto-generated method stub // // // // get selected radio button from radioGroup // int selectedId = yesno1.getCheckedRadioButtonId(); // radioButton= (RadioButton) findViewById(selectedId); // // switch (checkedId) { // case R.id.radioYes: // // Toast.makeText(Hello.this, // radioButton.getText(),Toast.LENGTH_SHORT).show(); // // break; // case R.id.radioNo: // // Toast.makeText(Hello.this, // radioButton.getText(),Toast.LENGTH_SHORT).show(); // // break; // default: // break; // } // // } // }); public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser){ switch (ratingBar.getId()) { case R.id.ratingBar1: rating1=String.valueOf(ratingBaR1.getRating()); Toast.makeText(getApplicationContext(), rating1, Toast.LENGTH_SHORT).show(); break; case R.id.ratingBar2: rating2=String.valueOf(ratingBaR2.getRating()); Toast.makeText(getApplicationContext(), rating2, Toast.LENGTH_SHORT).show(); break; case R.id.ratingBar3: rating3=String.valueOf(ratingBaR3.getRating()); Toast.makeText(getApplicationContext(), rating3, Toast.LENGTH_SHORT).show(); break; case R.id.ratingBar4: rating4=String.valueOf(ratingBaR4.getRating()); Toast.makeText(getApplicationContext(), rating4, Toast.LENGTH_SHORT).show(); break; case R.id.ratingBar5: rating5=String.valueOf(ratingBaR5.getRating()); Toast.makeText(getApplicationContext(), rating5, Toast.LENGTH_SHORT).show(); break; case R.id.ratingBar6: rating6=String.valueOf(ratingBaR6.getRating()); Toast.makeText(getApplicationContext(), rating6, Toast.LENGTH_SHORT).show(); break; case R.id.ratingBar7: rating7=String.valueOf(ratingBaR7.getRating()); Toast.makeText(getApplicationContext(), rating7, Toast.LENGTH_SHORT).show(); break; case R.id.ratingBar8: rating8=String.valueOf(ratingBaR8.getRating()); Toast.makeText(getApplicationContext(), rating8, Toast.LENGTH_SHORT).show(); break; case R.id.ratingBar9: rating9=String.valueOf(ratingBaR9.getRating()); Toast.makeText(getApplicationContext(), rating9, Toast.LENGTH_SHORT).show(); break; default: break; } } // button = (Button).findViewById(R.id.submit); // button.setOnClickListener(new View.OnClickListener() { // @Override public void onClick(View v) { Toast.makeText(Hello.this, "clicked on button", Toast.LENGTH_SHORT).show(); dialog = ProgressDialog.show(Hello.this, "", "Sending data to server", true); new Thread(new Runnable() { public void run() { sendData(); } }).start(); } // } // }); void sendData(){ try{ httpclient=new DefaultHttpClient(); httppost= new HttpPost("http://serverip:8080/ActCFWeb/FeedBackForm"); // make sure the url is correct. //add your data nameValuePairs = new ArrayList<NameValuePair>(8); // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar, nameValuePairs.add(new BasicNameValuePair("ans1",rating1)); // $Edittext_value = $_POST['Edittext_value']; nameValuePairs.add(new BasicNameValuePair("ans2",rating2)); nameValuePairs.add(new BasicNameValuePair("ans3",rating3)); nameValuePairs.add(new BasicNameValuePair("ans4",rating4)); nameValuePairs.add(new BasicNameValuePair("ans5",rating5)); nameValuePairs.add(new BasicNameValuePair("ans6",rating6)); nameValuePairs.add(new BasicNameValuePair("ans7",rating7)); nameValuePairs.add(new BasicNameValuePair("ans8",rating8)); nameValuePairs.add(new BasicNameValuePair("ans9",rating9)); // nameValuePairs.add(new BasicNameValuePair("ans10",rating9)); nameValuePairs.add(new BasicNameValuePair("username",name.getText().toString())); nameValuePairs.add(new BasicNameValuePair("mobile",mob.getText().toString())); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); //Execute HTTP Post Request System.out.println(response); ResponseHandler<String> responseHandler = new BasicResponseHandler(); final String response = httpclient.execute(httppost, responseHandler); System.out.println("Response : " + response); runOnUiThread(new Runnable() { public void run() { dialog.dismiss(); } }); if(response.contains("failed")){ runOnUiThread(new Runnable() { public void run() { Toast.makeText(Hello.this,"Data sent Successfully", Toast.LENGTH_SHORT).show(); } }); }else{ Toast.makeText(getApplicationContext(), "error while sending data to server", Toast.LENGTH_SHORT).show(); } }catch(Exception e){ dialog.dismiss(); System.out.println("Exception : " + e.getMessage()); } } } 时 我得到的警报是我在插件中加载的两个脚本

var src = $('body').html();
alert(src);

为什么我把它作为正文html而不是我页面的整个html?