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.