findAndReplaceDOMText icon indicating copy to clipboard operation
findAndReplaceDOMText copied to clipboard

Inject findAndReplaceDOMText onto page via Chrome Extension?

Open puyanwei opened this issue 6 years ago • 4 comments

Hi,

Is there a way of adding this to a chrome extension? I want to highlight all same words which involves me adding a span to those found.

I have tried to add it in the manifest.json like this;

"content_scripts": [
        {
            "matches": ["<all_urls>"],
            "js": ["lib/findAndReplaceDOMText.js", "content.js"]
        }
    ],

as well as trying to append it via background.js (preload) or content.js (page) with something like this;

let script = document.createElement("script");
script.setAttribute("src", "lib/findAndReplaceDOMText.js");
(document.head || document.documentElement).appendChild(script);
script.remove();

However the console says; findAndReplaceDOMText.js:7 Uncaught SyntaxError: Unexpected token <

puyanwei avatar May 27 '19 20:05 puyanwei

I've successfully included it in my Chrome extensions. Do you get that same console error in the background page console AND in the normal console when you load it as a content script? What about using the same file locally?

It just sounds like there's something wrong with the file itself.

jkupczak avatar May 28 '19 04:05 jkupczak

How did you set it up? Because this is suggested to add the file via a script tag in index.html.

I'll check when I get back, thx for the help so far

puyanwei avatar May 28 '19 16:05 puyanwei

Same way you did it.

"content_scripts": [
  {
    "matches": ["file:///*.html*"],
    "js": ["js/libs/findAndReplaceDOMText.js", "js/script.js"]
  }
]

I'm using version 0.4.5 of the script, not minified.

jkupczak avatar May 28 '19 22:05 jkupczak

Hmm weird. Yeah my error is saying

Uncaught ReferenceError: findAndReplaceDOMText is not defined
    at HTMLDocument.getSelectionText (content.js:16)
getSelectionText @ content.js:16

I also downloaded the unminified version and put that in my lib folder.

My code is here if you wanna have a quick look - https://github.com/puyanwei/chrome-extension-same-highlighter

Thx so far for ur help, really appreciate it!

puyanwei avatar May 29 '19 21:05 puyanwei