gpt4 book ai didi

javascript - 如何使用 Firefox 插件读取特定 URL 的 html 内容?

转载 作者:行者123 更新时间:2023-12-02 17:07:34 25 4
gpt4 key购买 nike

我想创建一个插件,它将加载特定网址的 html 内容并保存该页面的特定行,然后移动到该网址。我在 Mozila.org 上阅读了很多有关网页内容的内容,但我不明白如何阅读 html 内容。

最佳答案

这是一个执行 XHR 请求的简单代码片段,没有 cookie。不用担心跨域,因为您是从特权范围运行的,这意味着您不是在网站中编码,而是作为 Firefox 插件进行编码。

var {Cu: utils, Cc: classes, Ci: instances} = Components;
Cu.import('resource://gre/modules/Services.jsm');
function xhr(url, cb) {
let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);

let handler = ev => {
evf(m => xhr.removeEventListener(m, handler, !1));
switch (ev.type) {
case 'load':
if (xhr.status == 200) {
cb(xhr.response);
break;
}
default:
Services.prompt.alert(null, 'XHR Error', 'Error Fetching Package: ' + xhr.statusText + ' [' + ev.type + ':' + xhr.status + ']');
break;
}
};

let evf = f => ['load', 'error', 'abort'].forEach(f);
evf(m => xhr.addEventListener(m, handler, false));

xhr.mozBackgroundRequest = true;
xhr.open('GET', url, true);
xhr.channel.loadFlags |= Ci.nsIRequest.LOAD_ANONYMOUS | Ci.nsIRequest.LOAD_BYPASS_CACHE | Ci.nsIRequest.INHIBIT_PERSISTENT_CACHING;
//xhr.responseType = "arraybuffer"; //dont set it, so it returns string, you dont want arraybuffer. you only want this if your url is to a zip file or some file you want to download and make a nsIArrayBufferInputStream out of it or something
xhr.send(null);
}

此代码段的示例用法:

var href = 'http://www.bing.com/'
xhr(href, data => {
Services.prompt.alert(null, 'XHR Success', data);
});

关于javascript - 如何使用 Firefox 插件读取特定 URL 的 html 内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25109620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com