‌‌‌‌‌Get the context of the selected text

‌‌‌‌‌‌Hello everyone, I am currently trying to develop a plugin for PopClip, and I would like to be able to access the context of the selected word. However, after reviewing the PopClip development documents(such as https://www.popclip.app/dev/script-variables) and searching online, it seems that there are no variables or APIs available to access the word context.

I was wondering if PopClip has this capability at all. I am working on a context-based learning program, so the context of the selected word is crucial for my work.

eg:
This website aims to be a complete resource for PopClip users. It contains all the documentation for PopClip in one place, with the following sections
(when I select this word of documentation, I also want get the sentence It contains all the documentation for PopClip in one place)

1 Like

I can see how that would be very useful, but it’s not part of PopClip’s current capabilities to see text outside the actual text selected.

1 Like

Greatly appreciate your timely feedback. I am currently developing a learning tool for non-native speakers like myself. The tool helps them add new words encountered to Anki. Contextual learning greatly enhances the learning efficiency, which I personally enjoy. I use the online-dictionary-helper chrome extension to easily add new words, but it has limitations on browsers. Recently, I’ve come to realize the vast potential of popclip, which can break the browser’s restrictions. Therefore, I aim to develop a better tool to assist non-native, heavy Anki users like myself.

‌‌‌‌‌‌The plugin I mentioned is able to extract the context of the selected word(s) on a browser page. It would indeed be a big challenge for non-browser environments.

One possibility right now, though a little clunky, is you could write a module-based javascript extension with two actions, one to input the specific word and another to input the context.

Unlike other action types, JavaScript modules allow you to actually store variables which persist between invocations of the action. An example of an extension that already does this is the ChatGPT one (source), which stores the chat history between invocations.

If you need any pointers how to go about this let me know. I can write up an simpler example when I have a bit of time.

1 Like

thanks, I will look into the matter you mentioned right away.

A simple example would be greatly appreciated, as I am still a novice at this.

1 Like

Something like this:

// # popclip example of two actions sharing data
// name: Word & Context
// language: javascript
// module: true

let word = null;
let context = null;

function checkInputs() {
  if (word && context) {
    doSomething(word, context);
    // clear inputs for next time
    word = null;
    context = null;
  }
}

function doSomething(word, context) {
    // insert functionality here!
    popclip.showText(`Word is '${word}', context is '${context}'`);
}

exports.actions = [
  {
    title: "Input Word",
    code() {
      word = popclip.input.text;
      checkInputs();
    },
  },
  {
    title: "Input Context",
    code() {
      context = popclip.input.text;
      checkInputs();
    },
  },
];

(The above block is an extension snippet — select it then click “Install Extension” in PopClip.)