0

I'm trying to modify a webextension and running into problems. I have this in 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);
            });
        });
    });
}

And I want to use the output from the function textget() in another function (makeCaptureRequest). textget() needs to be executed in the browser, hence it is inside executeScript. I've tried a file too, and that didn't work either.

// code: "window.getSelection().toString();" this was the original code in the function.

the function textget() does return something, and it's like this in 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};

And background.js imports it import { textget } from "./copy"

Whatever I do, selection is always empty. I want the value in it to match text from the imported function.

  • The first line of copy.js should be `window.textget = () => {` and no need for the last `export`, no need to import it in background.js – wOxxOm May 01 '20 at 16:51
  • I would then have to use `file: "copy.js"`, right? – Latex_xetaL May 01 '20 at 17:02
  • I thought you already load it in manifest.json using `content_scripts`. If you don't then you can use `code` with a stringified function ([example](https://stackoverflow.com/a/4532567)) - in your case inContent will be textget) – wOxxOm May 01 '20 at 17:07
  • So I don't have to use `window.textget = () => {`, right? My code looks like this now: `chrome.tabs.executeScript( {code: \`(${ textget })\`}, selections => { \\rest unchanged` – Latex_xetaL May 01 '20 at 18:36
  • Right now, the only line I'm changing is `code: "window.getSelection().toString();"` to `\code: `(${ textget })\``, but it doesn't work. I suppose there's something else broken... Thanks for the help. – Latex_xetaL May 01 '20 at 19:52
  • Ah sorry I didn't notice that you've deleted half of the string. You need to add `()` so it looks like \`(${ textget })()\` – wOxxOm May 02 '20 at 04:47

0 Answers0