从外部页面中将文本片段导入Google Chrome扩展程序 [英] Getting Snippet Of Text From External Page Into Google Chrome Extension

查看:140
本文介绍了从外部页面中将文本片段导入Google Chrome扩展程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个简单的Google Chrome扩展程序。它的功能是显示页面上的下载次数(计数),并显示在弹出窗口中。



需要监控的页面是一个页面只能在登录后才能进行处理。



因此,我需要基本上使用html和javascript来从页面中报废计数并将其显示在弹出窗口中。到目前为止,我已经创建了exntension的准系统:

  {
name:Downloads Logger,
version:1.0,
manifest_version:2,
description:监视下载次数,
browser_action:{
default_icon:icon.png
},
权限:[
http://exampledownloadpage.com/loggedin/
]
}

接下来我该做什么?我需要扩展的后台页面吗?我已经阅读了扩展文档并解开了正式的gmail扩展,并尝试了解它是如何工作的,但它似乎利用'feeds'来获取它的数据。任何人都可以指引我走向正确的方向吗?如果您只想在browser_action中显示下载次数,那么您不需要一个背景页面,但如果你想每隔一分钟左右监视一次下载量,那么你就可以。
要获得下载次数,您可以使用XMLHttpRequest下载页面源代码,然后使用.....将其解析为dom。

  var page = document.implementation.createHTMLDocument(); 
page.documentElement.innerHTML = pageSource; //假设您使用httprequest下载的页面的源代码位于pageSource中

...然后您可以使用诸如querySelector之类的东西来获取你所使用的页面的文本,比如..... $ / downloadstats = page.querySelector('。downloadstats')。textContent.trim();

希望这会让您开始。



<这是一个简单的(没有错误检查或不是什么)的例子,它可以从你的stackoverflow得到你的代理点.....

  var xmlhttp = new XMLHttpRequest(); 
xmlhttp.onreadystatechange = function(){
if(this.readyState == 4){
var page = document.implementation.createHTMLDocument();
page.documentElement.innerHTML = this.responseText;
var score = page.querySelector('[title ^ =your reputation;]')。textContent.trim();
alert('你有'+分数+'rep points'。);
}
}
xmlhttp.open(GET,http://stackoverflow.com,true);
xmlhttp.send();


I'm try to develop a simple google chrome extension. What it does it that it displays the number of downloads on a page (the count) and display it in a popup.

The page in question that needs monitoring is a page which can only be acessed after logged in.

So I need to basically use html and javascript to 'scrap' the count number from the page and display it in a popup. So far I have created the barebones of the exntension:

{
  "name": "Downloads Logger",
  "version": "1.0",
  "manifest_version": 2,
  "description": "Monitors Number Of Downloads",
  "browser_action": {
    "default_icon": "icon.png"
  },
  "permissions": [
    "http://exampledownloadpage.com/loggedin/"
  ]
}

What do I do next? Do I need a background page for the extension? I have read up the extensions documentation and unpacked the official gmail extension and try seeing how it works, but it seems to make use of 'feeds' to get it's data. Can anybody point me in the right direction? Thank you in advance!

解决方案

If you only want to display the number of downloads in a browser_action then you shouldnt need a background page, but if you wanted to monitor the amount of downloads every minute or so then you would. To get the download count you could use XMLHttpRequest to download the pages source, then parse it to a dom using.....

var page = document.implementation.createHTMLDocument("");
page.documentElement.innerHTML = pageSource;  // assuming the source for the page you downloaded with httprequest is in pageSource

...and then you could use something like querySelector to get the text of the bit of the page your intesersted in, something like.....

downloadstats = page.querySelector('.downloadstats').textContent.trim();

Hopefully that'll get you started.

Here's a simple (no error checking or what not) example that gets your rep points from stackoverflow.....

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4) {
        var page = document.implementation.createHTMLDocument("");
        page.documentElement.innerHTML = this.responseText;
        var score = page.querySelector('[title^="your reputation;"]').textContent.trim();
        alert('You have ' + score + ' rep points.');
    }
}
xmlhttp.open("GET", "http://stackoverflow.com", true);
xmlhttp.send();

这篇关于从外部页面中将文本片段导入Google Chrome扩展程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆