Home > database >  How can I click on word, make it be searched in a program that was opened by that click?
How can I click on word, make it be searched in a program that was opened by that click?

Time:12-27

I want to have a word, which is clickable. On click a program should be opened automatically with the clicked word searched. For reference this program should be opened http://ebstudio.info/manual/EBWin4/EBWin4.html. I tried

<script type="text/javascript" language="javascript">
    function RunFile() {
    WshShell = new ActiveXObject("WScript.Shell");
    WshShell.Run(""C:\Program Files\EBWin4(x64)\"", 1, false);
    }
</script>

 <div ><p><a href="javascript:RunFile()">温恭</p></div>

The word 温恭 is displayed. On click the word should be searched in the program EBWin4.

CodePudding user response:

You need to pick the word and then add event listener :

const word = document.querySelector("#test")
//add an <a id ='test'> test </a> (put the id you want insted of test)

More about it here

then :

word.addEventListener("click", () => {
 RunFile() //run the function you want to run when user is clicking it
});

more about it here

CodePudding user response:

If you have not many words, you can add event listeners to them. But if you have a whole book, it is impossible to do. Then you can use windows.get Selection() Also, I need to mention if you have formatting tags that break a word, you will get the part of the word you clicked on, but not the entire word. Then you need to add some extra logic. If you have the word, you can use it in your function to do other manipulations.

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<div id="hello" onclick="doSomething()">
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus, aliquam asperiores aspernatur assumenda consectetur corporis eum eveniet laboriosam laborum nihil officiis sapiente, vel veritatis vitae voluptate. Assumenda corporis, illum! Aspernatur?
</div>
</body>
<script>
  function  doSomething () {
        const selection = window.getSelection();       
        const text = selection.anchorNode.textContent;
        const lmatch = text.substr(0, selection.anchorOffset).match(/[\s\S]*\s/);
        const offset = lmatch ? lmatch[0].length : 0;
        const match = text.substr(offset).match(/\w /);
        console.log(match && match[0]);
    }
</script>
</html>

  • Related