【发布时间】:2019-04-06 01:18:33
【问题描述】:
我查看了其他相关的 SO 帖子,但解决方案没有帮助解决我的问题。这是我的第一个 chrome 扩展,所以请多多包涵!
我正在编写一个简单的 chrome 扩展程序,用于在网页上搜索用户提供的关键字。我无法获取返回要运行的 DOM 内容的内容脚本。一些代码,我取自另一个 SO 帖子中的答案,但我似乎无法让它为我工作。
我在文件顶部放了一个console.log("hello world"),但是没有显示出来,所以我想可能是我项目的结构。
manifest.json
{
"name": "keyword search",
"version": "0.0.1",
"manifest_version": 2,
"permissions": [ "tabs" , "storage", "activeTab", "<all_urls>"],
"browser_action": {
"default_popup": "html/form.html"
},
"content_scripts": [{
"matches": [ "<all_urls>" ],
"js": [ "js/jquery.min.js", "content_scripts/content_script.js" ]
}],
"homepage_url": "http://google.com/"
}
js/popup.js
function run() {
running = true;
console.log('running');
var url = "https://www.stackoverflow.com/"
// Get KW & category for search
chrome.storage.local.get(["kw"],
function (data) {
kw = data.kw;
console.log("redirecting to find kw: " + kw);
// Send current tab to url
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.update(tabs[0].id, {url: url});
chrome.tabs.sendMessage(tabs[0].id, {type: 'DOM_request'}, searchDOM);
});
}
);
}
function searchDOM(domContent) {
console.log("beginning dom search \n" + domContent);
}
content_scripts/content_script.js
// Listen for messages
console.log("hello world")
chrome.runtime.onMessageExternal.addListener(function (msg, sender, sendResponse) {
// If the received message has the expected format...
if (msg.type === 'DOM_request') {
// Call the specified callback, passing
// the web-page's DOM content as argument
sendResponse(document.all[0].outerHTML);
}
});
控制台
running
redirecting to find kw: TestKeyword
beginning dom search
undefined
【问题讨论】:
标签: google-chrome google-chrome-extension