Привет, незнакомец!

Похоже, вы здесь новенький. Чтобы принять участие, нажмите одну из кнопок ниже!

Got malformed xml via GM.xmlHttpRequest?

Раздел: Разработка [?]

I'm trying to check in the background if a linked website has the text "Gewerblicher Nutzer" (Commercial user) at a certain position. The xpath is correct, I ran several different tests.

But apparently the problem is further up, I only get a parsererror back.

What can I do, where is the mistake? Hopefully not some async/promise problem again :dizzy:

// ==UserScript==
// @name         Ebay-kleinanzeigen.de Gewerbliche kennzeichnen
// @namespace    graphen
// @version      1.0.0
// @description  Kennzeichnet oder versteckt Suchergebnisse von gewerblichen Verkäufern
// @author       Graphen
// @match        https://www.ebay-kleinanzeigen.de/*
// @noframes
// @grant        GM.xmlHttpRequest
// ==/UserScript==

/* jshint esversion: 6 */
(function (doc) {
    'use strict';

    function requestHTML(adURL) {
        GM.xmlHttpRequest({
            method: "GET",
            url: adURL,
            onload: function(response) {
                var responseXML = null;
                // Inject responseXML into existing Object (only appropriate for XML content).
                if (!response.responseXML) {
                    responseXML = new DOMParser()
                        .parseFromString(response.responseText, "text/xml");
                }
                isCommercial(response.responseXML);
                console.log("XML response:");
                console.log(response.responseXML);
            }
        });
    }
    function isCommercial(xml) {
        let xpath = "normalize-space(//div[@id='viewad-contact']/div/ul/li[1]/span/span[@class='text-light']/text()[1])";
        var result = xml.evaluate(xpath, xml, null, XPathResult.ANY_TYPE, null);
        //STRING_TYPE
        console.log("xPath evaluate result:");
        console.log(result);
        if (result.stringValue === "Gewerblicher Nutzer") {
            return true;
        }
        else {
            return false;
        }
    }
    requestHTML("https://www.ebay-kleinanzeigen.de/s-anzeige/peugeot-belville-125-allure-abs/1123738805-305-6719");


})(document);

Комментарии

  • Try to actually use your fallback code for xml parser:

            onload: function(r) {
                var responseXML = r.responseXML ||
                  new DOMParser().parseFromString(r.responseText, "text/xml");
                isCommercial(responseXML);
                console.log("XML response:");
                console.log(responseXML);
            }
    
  • Thanks for your answer. I tried it and it's the same result :(
    But I noticed that the XML response URL isn't even the one specified in requestHTML("https://blabla.bla") but the one from the site you are currently at:
    https://i.imgur.com/F7ELPH6.png
    I hope that's just a consequence of the parsererror because I have no idea why that should happen.

  • It means you can't use XML parser on an HTML document.
    Use DOMParser with text/html:

        onload: function(r) {
            var doc = new DOMParser().parseFromString(r.responseText, "text/html");
            isCommercial(doc);
            console.log(doc);
        }
    
  • oh niceee it works :)
    Thanks a lot!

Войдите или Зарегистрируйтесь чтобы комментировать.