【发布时间】:2017-01-12 01:17:47
【问题描述】:
我正在开发一个用于对 Google 搜索结果进行文本解析的 Chrome 扩展程序。我希望用户在多功能框中插入特定文本,然后直接进入 Google 搜索页面。
function navigate(url) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.update(tabs[0].id, {url: url});
});
}
chrome.omnibox.onInputEntered.addListener(function(text) {
navigate("https://www.google.com.br/search?hl=pt-BR&lr=lang_pt&q=" + text + "%20%2B%20cnpj");
});
alert('Here is where the text will be extracted');
将当前标签指向搜索页面后,我想获取页面的纯文本形式,然后进行解析。实现此目的最直接的方法是什么?
【问题讨论】:
-
使用content script。如果您阅读了extensions overview(这很重要),您应该知道这一点,我在评论中将其链接到您之前的问题。
-
你可以找到很多例子。这是一个:How to check how many elements of a certain type has a page with Chrome Extension Dev - 只是不要忘记只能传递普通对象(不是 DOM 元素),所以使用例如
document.body.innerHTML用于code参数或[].map.call(document.querySelectorAll('div.rc a'), function(a) { return {url: a.href, text: a.textContent} });
标签: javascript google-chrome google-chrome-extension