簡體   English   中英

如何使用Firefox插件讀取特定URL的html內容?

[英]How to read html content of a specific URL using Firefox addon?

我想創建一個加載項,該加載項將加載特定網址的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);
});

在不知道要查找的頁面和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');
}

用法:

  • 如果尚未安裝Greasemonkey ,請安裝它。
  • 創建類似於我的腳本。 將@include的值設置為包含要查找的URL的頁面。
  • 您必須確定用目標URL標識頁面部分的內容,然后更改腳本以找到該URL。 對於我的腳本,這是目標為“ _blank”的鏈接。

保存腳本后,訪問帶有鏈接的頁面。 Greasemonkey應該執行您的腳本並重定向瀏覽器。

[edit]這會在腳本標簽中搜索您所描述的文本並進行重定向。

// ==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];
        }
    }
}

查找URL的正則表達式可能需要進行一些調整以匹配確切的頁面內容。

Addon或GreaseMonkey腳本具有類似的方法,但是addon可以使用本機Firefox API。 (但是比腳本要復雜得多)

基本上,這就是過程(不知道您的確切要求)

  1. 使用XMLHttpReques()獲取遠程URL的內容

  2. 使用RegEx或DOMParser()獲取所需的數據

  3. 使用location.replace()將當前URL更改為該目標。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM