I have a simple chrome extension that grabs a user’s selected text using Chrome Tabs API. I’d like to import a Hugging Face API eventually, but when I import the required model, my Chrome Tabs API no longer executes. The selected text no longer appears inside the p tag I created in HTML. I tried wrapping the import line in tabs and it still does not work. I am wondering if anyone can help to see why importing this is causing the rest of my code to not execute properly?
JAVASCRIPT
chrome.tabs.executeScript( {
code: "window.getSelection().toString();"
}, function(selection) {
document.getElementById("new-task").innerHTML = (selection[0])
});
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@xenova/[email protected]';
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p type="text" id="new-task" class="new-task" contenteditable="true"></p>
</body>
</html>
JSON
{
"manifest_version": 2,
"name": "X",
"version": "1.0",
"permissions": [
"activeTab",
"storage",
"https://ajax.googleapis.com/"
],
"browser_action": {
"default_popup": "index.html"
},
"background": {
"scripts": ["myscript.js"]
}
}
2
Answers
The first thing to check is if there are any errors or issues with the imported module from Hugging Face. If there is a problem with that module, it might break the rest of your code.
The problem you’re having appears to be due to the way the JavaScript code is constructed. When you use
import
to import external dependencies, it should not interfere with your current code. Make sure the code that utilizes the Chrome Tabs API comes after the import statement. After the needed model has been imported, thechrome.tabs.executeScript
method should be invoked. Examine the model for any mistakes that may have occurred during the import process. JavaScript mistakes can prevent future code from running. Here’s a rewritten version of your code that takes these factors into account:You ensure that your code runs after the DOM has fully loaded by surrounding it within the
DOMContentLoaded
event listener. It also checks for any issues that may arise during the execution ofchrome.tabs.executeScript
.