Using Plugins in Chrome Extension

一笑奈何 提交于 2019-12-13 21:42:28

问题


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:

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


回答1:


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



来源:https://stackoverflow.com/questions/38178774/using-plugins-in-chrome-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!