Extension problem for Google Chrome

Asked

Viewed 106 times

1

Good afternoon, My goal is to make an extension to monitor price, but the code is not working. It was to generate a txt file. also wanted to know how I do to display information in the popup window. I appreciate any kind of help. tutorial I’m following

manifest.json

{
"name": "Get pages source",
"version": "1.0",
"manifest_version": 2,
"description": "Pega o conteudo da página e envia para um servidor",
"browser_action": {
   "default_icon": "icon.png",
   "default_popup": "popup.html"
},
"permissions": [
    "webRequest",
    "tabs",
    "clipboardWrite",
    "clipboardRead",
    "https://www.extra.com.br/?Filtro=D33183&Ordenacao=_MaisVendidos&icid=140200_hp_stc_c7_ps1_b0_pb1"
]
}

popup.js

chrome.extension.onMessage.addListener(function(request, sender) {
if (request.action === "getSource") {
    var message, data, xhr;

    message = document.querySelector("#message");
    data = request.source;

    message.innerText = "Enviando ao servidor...";

    xhr  = new XMLHttpRequest();
    xhr.open("POST", "http://site1/webservice.php", true);
    xhr.onreadystatechange = function() {
        if(xhr.readyState === 4) {
            if (xhr.status === 200) {
                message.innerText = "Resposta do servidor: " + xhr.responseText;
            } else {
                message.innerText = "Err: " + xhr.status;
            }
        }
    };

    //Enviando dados como RAW
    xhr.send(request.source);
}
 });

function onWindowLoad()
{
var message = document.querySelector('#message');

chrome.tabs.executeScript(null, {
    file: "getPagesSource.js"
}, function() {
    // If you try and inject into an extensions page or the webstore/NTP you'll get an error
    if (chrome.extension.lastError) {
        message.innerHTML = "Erro ao executar o script : <br>" + chrome.extension.lastError.message;
    }
});
}

window.onload = onWindowLoad;

getPagesSource.js

function DOMtoString(document_root) {
var html = '',
    node = document_root.firstChild;
while (node) {
    switch (node.nodeType) {
    case Node.ELEMENT_NODE:
        html += node.outerHTML;
        break;
    case Node.TEXT_NODE:
        html += node.nodeValue;
        break;
    case Node.CDATA_SECTION_NODE:
        html += '<![CDATA[' + node.nodeValue + ']]>';
        break;
    case Node.COMMENT_NODE:
        html += '<!--' + node.nodeValue + '-->';
        break;
    case Node.DOCUMENT_TYPE_NODE:
        // (X)HTML documents are identified by public identifiers
        html += "<!DOCTYPE " + node.name + (node.publicId ? ' PUBLIC "' + node.publicId + '"' : '') + (!node.publicId && node.systemId ? ' SYSTEM' : '') + (node.systemId ? ' "' + node.systemId + '"' : '') + '>\n';
        break;
    }
    node = node.nextSibling;
}
return html;
}
chrome.extension.sendMessage({
action: "getSource",
source: DOMtoString(document)
});

webservice.php

<?php

if (false === ($input = fopen('php://input', 'r'))) {
echo 'Erro ao ler os dados recebidos';
} else if (false === ($output = fopen('meu-arquivo.txt', 'w'))) {
echo 'Erro abrir arquivo para gravação';
fclose($input);
$input = NULL;
}else {
$hasData = false;

while (false === feof($input)) {
    $data = fgets($input, 128);
    if ($data !== '') {
        $hasData = true;
    }

    fwrite($output, $data);
}

fclose($input);
fclose($output);

$input = $output = NULL;

echo $hasData ? 'Ok' : 'Área de seleção vazia, tente novamente';
}
?> 
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.