Как мне go получить значение из функции в executeScript? - PullRequest
0 голосов
/ 01 мая 2020

Я пытаюсь изменить webextension и сталкиваюсь с проблемами. У меня это в background.js:

function capture(comment: ?string = null, tag_str: ?string = null) {
    chrome.tabs.query({currentWindow: true, active: true }, tabs => {
        const tab = tabs[0];
        if (tab.url == null) {
            showNotification('ERROR: trying to capture null');
            return;
        }
        const url: string = tab.url;
        const title: ?string = tab.title;

        get_options(opts => {
               chrome.tabs.executeScript( {
                // code: "window.getSelection().toString();"
                // file: "copy.js"
                code: "textget();"
            }, selections => {
                const selection = selections == null ? null : selections[0];
                makeCaptureRequest({
                    url: url,
                    title: title,
                    selection: selection,
                    comment: comment,
                    tag_str: tag_str,
                }, opts);
            });
        });
    });
}

И я хочу использовать вывод из функции textget () в другой функции (makeCaptureRequest). textget () должен быть выполнен в браузере, поэтому он находится внутри executeScript. Я тоже пробовал файл, и он тоже не работал.

// code: "window.getSelection().toString();" это был оригинальный код функции.

функция textget() что-то возвращает, и это так в copy.js

function textget() {
    let title = window.title;
    if (options.titleSubstitution !== "") {
      let pattern = new RegExp(
        options.titleSubstitution
          .split(/\n/)
          .map(e => `(${RegexEscape(e)})`)
          .join("|"),
        "g"
      );
      title = title.replace(pattern, "");
    }
    let text = options.linkWithoutStyling ? `${title} (${window.URL})` : `[${title}](${window.URL})`;
    let selectionNew = getSelectionAsMarkdown(options);

    if (selectionNew.output !== "") {
      if (options["use-quote"]) {
        selectionNew.output = selectionNew.output
          .split("\n")
          .map(line => `> ${line}`)
          .join("\n");
      }
      if (options["link-to-source"]) {
        text += `\n\n${selectionNew.output}`;
      } else {
        text = selectionNew.output;
      }
    }

    return text;
}
export{textget};

И background.js импортирует его import { textget } from "./copy"

Что бы я ни делал, selection всегда пуст. Я хочу, чтобы значение в нем совпадало с text из импортированной функции.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...