0

I am making a Chrome extension and want to implement a plugin mark.js (https://markjs.io/) that would highlight text on the user page. However, I am having trouble importing the plugin. Since the html is the user's page and I don't have access to it, I can't use the usual <script></script>. So, I tried to use JavaScript to do it, but am still getting an error.

My JavaScript code is as follows:

var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.type= 'text/javascript';
script.src= 'https://cdnjs.cloudflare.com/ajax/libs/mark.js/7.0.0/mark.min.js';
head.appendChild(script);

//highlight necessities
var context = document.querySelector("body");
var instance = new Mark(context);

function handleSetQuery(findWord) {
    cheese.mark(findWord);
}

function handlePrevious() {
    //insert previous thing
}

function handleNext() {
    //insert next thing
}

function handleClear() {
    instance.unmark(options);
}

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
    if (request.action === "setquery") {
        alert("received: setquery");
        handleSetQuery(request.data);
    } else if (request.action === "previous") {
        handlePrevious();
    } else if (request.action === "next") {
        handleNext();
    } else if (request.action === "clear") {
        handleClear();
    }
});

The Chrome console is returning the following error:

Chrome Error

Any help is appreciated! Solving this issue would also help me use other tools like jQuery.

Rohan Khajuria
  • 636
  • 2
  • 12
  • 26
  • To insert scripts to pages that match a certain pattern, please read the following tutorial: https://developer.chrome.com/extensions/content_scripts#registration – dude Jul 04 '16 at 07:47
  • 2
    Possible duplicate of [Building a Chrome Extension - Inject code in a page using a Content script](http://stackoverflow.com/questions/9515704/building-a-chrome-extension-inject-code-in-a-page-using-a-content-script) – Haibara Ai Jul 04 '16 at 08:16
  • I tried those solutions, but was unable to get them to work. Could you tell me specifically what to do? – Rohan Khajuria Jul 04 '16 at 19:45

1 Answers1

0

I injected it via the content scripts tag (I previously had them in the wrong order).

Rohan Khajuria
  • 636
  • 2
  • 12
  • 26