2016-03-30 3 views
0

Как использовать JQuery в content.js.Как использовать jquery в скрипте содержимого chrome extension

Вот мой код

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 ['bname'] возвращает пустой ответ. Где, как, я получаю значение, когда я использую document.getElementById

+1

сначала вы можете проверить ваш файл Jquery подходит или нет на переднем конце страницы? ? Я проверяю ваш пример, который показывает пустое? –

+0

У меня нет главной страницы/popup.html. Я пытаюсь читать содержимое dom с загруженной вкладки – user2901901

ответ

-2

Добавление ниже линии в popup.html сделал трюк

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

Где находится строка? – itzmukeshy7