加载本地HTML文件时不会执行内容脚本

时间:2020-06-28 21:21:02

标签: javascript google-chrome-extension content-script

我想要一个“简单”扩展名:

  1. 它获取初始站点的一些信息
  2. 它会加载一个新站点(本地HTML文件)
  3. 它将新站点作为模板,并将信息放入 正确的地方。

我认为我只需要:

  • 内容脚本(从旧的信息中获取,保存和替换信息 到新站点)
  • 背景脚本(用于点击扩展名)
  • 清单
  • 本地HTML模板(用于加载它并将其用作模板)

主要问题是,即使将消息从后台脚本传递到内容脚本时,也没有错误消息,但新加载的网站(HTML模板)上的内容脚本没有任何输出。

内容脚本:

chrome.runtime.onConnect.addListener(function (port) {
    console.assert(port.name == "knockknock");
    port.onMessage.addListener(function (msg, port) {
        gotMessage(msg, port);
    });
});

function gotMessage(message, port) {
    if (message.txt == "Sammle Rechnungsinformationen") {
        getBestellInformationen();
        port.postMessage({ response: "true" });
    }
    else if (message.tee == "Aendere das Template") {
        setBestellInformationen();
        port.postMessage({ response: "true" });
    }
}


function getBestellInformationen() {
    let bestellInformationen = {};
    let tables = document.getElementsByTagName('td');

    for (elt of tables) {
        elt.style['background-color'] = '#FF00FF';
    }

    for (i = 0; i < tables.length; i++) {
        content = tables[i].textContent;

        switch (content) {
            ...
            case String(content.match("Net.Grand.Total:")):
                bestellInformationen["Gesamtbetrag Netto"] = (tables[i + 1].textContent);
                break;
        }
    }

    // Save the current myArray value.
    chrome.storage.local.set({ key: bestellInformationen }, function () {
        console.log('Value is set to ' + bestellInformationen);
    });
}

function setBestellInformationen() {

    // Retrieve the the stored value, defaulting to an empty array.
    chrome.storage.local.get(['key'], function (result) {
        console.log('Value currently is ' + result.key);

        let bestellInformationen = result.key;

        // Aendere das geoeffnete Template
        console.log("Template Bearbeiten");

        console.log(bestellInformationen);

        for (key in bestellInformationen) {
            console.log(bestellInformationen[key]);
        }
    });

    chrome.storage.local.clear(function () { console.log("storage cleared successfully"); });
}

背景脚本: chrome.browserAction.onClicked.addListener(buttonClicked);

function buttonClicked(tab) {
    var port = chrome.tabs.connect(tab.id, { name: "knockknock" });

    if (!((String(tab.url.match("\X*Die_HTML_Datei.html"))) == "Die_HTML_Datei.html")) {
        let msg = { txt: "Sammle Rechnungsinformationen" };

        port.postMessage(msg);
        port.onMessage.addListener(function (msg) {
            if (msg.response == "true") {
                oeffneDieRechnungHTML();
                port.disconnect();
        });

    } else {
        let msg = {txt: "Aendere das Template" };

        port.postMessage({ txt: "Aendere das Template" });
        port.onMessage.addListener(function (msg) {
        });
        port.disconnect();
    }
}

function oeffneDieRechnungHTML() {
    chrome.tabs.update({ url: chrome.runtime.getURL("Die_HTML_Datei.html") }, 
         function () { console.log("Seite wurde Erfolgreich zur HTML-Rechnung gewechselt") });
}

清单:

{
  "manifest_version": 2,
  "name": "Codingtrain",
  "version": "0.1",
  "permissions": [ "activeTab", "tabs", "storage" ],
  "web_accessible_resources": [ "Die_HTML_Datei.html" ],
  "content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "js": [ "content.js" ]
    }
  ],

  "background": {
    "scripts": [ "background.js" ]
  },

  "browser_action": {
    "default_icon": "get_started48.png"
  }
}

编辑:改写为更容易理解。

0 个答案:

没有答案
相关问题