Javascript 如何使用Firefox插件读取特定URL的html内容?
我想创建一个插件,它将加载特定url的html内容,并保存该页面的特定行,然后移动到该url。我在Mozila.org上读了很多关于网页内容的东西,但我不知道如何阅读html内容 插件或GreaseMonkey脚本有类似的方法,但插件可以使用本机Firefox API。(但它比脚本复杂得多) 基本上,这就是过程(不知道您的确切要求)Javascript 如何使用Firefox插件读取特定URL的html内容?,javascript,html,firefox-addon,Javascript,Html,Firefox Addon,我想创建一个插件,它将加载特定url的html内容,并保存该页面的特定行,然后移动到该url。我在Mozila.org上读了很多关于网页内容的东西,但我不知道如何阅读html内容 插件或GreaseMonkey脚本有类似的方法,但插件可以使用本机Firefox API。(但它比脚本复杂得多) 基本上,这就是过程(不知道您的确切要求) 使用XMLHttpRequests() 使用RegEx或DOMParser() 使用location.replace() 下面是一个简单的代码片段,它不需要cook
XMLHttpRequests()
DOMParser()
location.replace()
下面是一个简单的代码片段,它不需要cookies就可以执行XHR请求。不要担心跨源代码,因为您是从私有范围运行的,这意味着您不是在网站中编写代码,而是作为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);
}
此代码段的示例用法:
如果不知道要在上面查找的页面和URL,我无法创建一个完整的解决方案,但下面是我编写的Greasemonkey脚本示例,它也有类似的功能 此脚本用于DZone上的Java文章。当文章具有指向源的链接时,它将重定向到此源页面:
// ==UserScript==
// @name DZone source
// @namespace com.kwebble
// @description Directly go to the source of a DZone article.
// @include http://java.dzone.com/*
// @version 1
// @grant none
// ==/UserScript==
var node = document.querySelector('a[target="_blank"]');
if (node !== null) {
document.location = node.getAttribute('href');
}
用法:
- 如果尚未安装,请安装
- 创建脚本,类似于我的脚本。将@include的值设置为包含要查找的URL的页面
- 您必须确定用目标URL标识页面部分的内容,并更改脚本以查找该URL。对于我的脚本,它是一个链接,目标为“_blank”
// ==UserScript==
// @name Test
// @namespace com.kwebble
// @include your_page
// @version 1
// @grant none
// ==/UserScript==
var nodes = document.getElementsByTagName('script'),
i, matches;
for (i = 0; i < nodes.length; i++) {
if (nodes.item(i).innerHTML !== '') {
matches = nodes.item(i).innerHTML.match(/windows\.location = "(.*?).php";/);
if (matches !== null){
document.location = matches[1];
}
}
}
/==UserScript==
//@name测试
//@namespace com.kweble
//@包括您的页面
//@version 1
//@grant none
//==/UserScript==
var nodes=document.getElementsByTagName('script'),
i、 火柴;
对于(i=0;i
查找URL的正则表达式可能需要进行一些调整以匹配确切的页面内容。它必须是一个附加组件,可以由其他人安装,还是足以在您的计算机上运行?我正在考虑使用greasemonkey脚本。好的,谢谢你的详细回答,让我试试这个!我的荣幸。这是复制粘贴。您可以将其复制粘贴到草稿行,将“环境”菜单设置为“浏览器”,然后运行。请记住,这不会使用用户的cookies。再想一想,sdk有一些内置的xhr模块,请参见这里,它被称为请求模块。如果您使用的是sdk,那么您应该这样做:no no lol如果您想使用我在上面粘贴的代码,请跟随我在上面的注释。如果您想使用请求模块复制,请将该站点的示例代码粘贴到main.jsPaging@canuckistani,需要插件sdk专家。我想从页面标题获取url。url在javascript中写为
windows.location=”http://www.url.com/blah_blah.php";代码>我在页面中添加了搜索脚本内容的第二个版本。好的,这非常有用!谢谢我需要更多的帮助,这里只有一个windows.location,但每次都有不同的url。我可以复制没有任何匹配的url吗?最后,我想移动这个链接。怎么办?不同的URL是可以的,代码(.*)表示该位置的任何值都被视为URL。我不明白你所说的“复制url而不匹配”和移动链接是什么意思。如果正则表达式匹配,则matches[1]的值包含URL,请随意使用它。
// ==UserScript==
// @name Test
// @namespace com.kwebble
// @include your_page
// @version 1
// @grant none
// ==/UserScript==
var nodes = document.getElementsByTagName('script'),
i, matches;
for (i = 0; i < nodes.length; i++) {
if (nodes.item(i).innerHTML !== '') {
matches = nodes.item(i).innerHTML.match(/windows\.location = "(.*?).php";/);
if (matches !== null){
document.location = matches[1];
}
}
}