Search code examples
javascripttampermonkey

Console Error, need to remove a specific character


I know the title may be confusing, but my problem seems fairly easy.

I'm trying to remove a specific character "#".

This is how it currently logs (Click Here = Image) I'm trying to simply remove the "#" character so when it logs to the console it won't have the # in front of the text.

I've tried adding this before the break.

var color = stroke.split("#"); but it didn't work.

Here is my code

xhr = new XMLHttpRequest();
xhr.open("GET", "http://colorillo.com/byjy.inline.svg");
xhr.addEventListener("load", function() {
const xmlDoc = new DOMParser().parseFromString(
    this.responseText.trim(),
    "image/svg+xml"
);
// Get all polylines as strings
let lines = xmlDoc.getElementsByTagName('polyline');
// Loop over all lines
for(let line of lines) {
    // --- OPTIONAL START ---

    //See Bruce'es comment (rgb() as output)
    let stroke = line.style.stroke;
    //let size = line.style.stroke-width;  -- Disabled
    // --- OR ---

    // Loop over all styles of this line (output same as input [hex])
    for(let style of line.getAttribute('style').split(';')) {
        // Get name & value
        let valueOffset = style.indexOf(':');
        // Check if name equal to 'stroke'
        if(style.substr(0, valueOffset).trim() === 'stroke') {
            // Save stroke value
            stroke = style.substr(valueOffset + 1).trim();
            // Break out of the loop (we don't have to search further)
            stroke.split("#"); // Here is what I've tried.
            break;
        }
    }
    console.log(stroke)
}
});
xhr.send();

Solution

  • Change stroke.split("#") to

    stroke.replace('#','');
    

    This will remove the first '#' in the string.

    You can confirm by adding

    console.log('BEFORE: '+stroke);
    

    before that line, and

    console.log('AFTER: '+stroke);
    

    after it. Then check your console.log output in Dev Tools (or equivalent).