如何在内容脚本chrome扩展中使用jquery

时间:2016-03-30 06:52:13

标签: javascript jquery google-chrome-extension

如何在content.js中使用Jquery。

这是我的代码

的manifest.json

    "background": {
    "scripts": ["jquery-1.12.2.min.js","background.js"]
  },
  "content_scripts": [{
    "matches": [ "http://*/*", "https://*/*"],
    "js": ["jquery-1.12.2.min.js","content.js"]
  }],
    "permissions": [
    "tabs",
    "activeTab",
    "http://*/*",
    "https://*/*",
    "<all_urls>"

  ]

background.js

chrome.tabs.create({url: "https://www.google.com/search?gws_rd=cr&as_qdr=all&q=" + encodeURIComponent(searchQ)}, function (tab){openSearchLink(tab)});
var createdTabId = 0;
function openSearchLink(tab){
    console.log("new tab "+tab.id);
    createdTabId = tab.id;

}

content.js

// Listen for messages
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
    // If the received message has the expected format...
    if (msg.text === 'report_back') {
        // Call the specified callback, passing
        // the web-page's DOM content as argument
        var resAry = {};
        console.log('dddd-'+$('#bname').val());
        resAry['bName'] = $('#bname').val().trim();
        sendResponse(resAry);
    }
});

resAry [&#39; bname&#39;]返回空响应。在哪里,当我使用document.getElementById

时,我正在获得价值

1 个答案:

答案 0 :(得分:-2)

在popup.html中添加以下行就可以了解

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