0

I'm trying to run a JavaScript code "Bookmarklet" But I could not do it, someone knows how to do it?

background.js

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.executeScript(tab.id, {file: "sc-dl.js"})
});

var executeScript = function(info, tab){
    chrome.tabs.executeScript(null, {file:"sc-dl.js"});
}
var Test2 = {"title": "Test2", "contexts":["page"], "parentId":menu,"onclick":executeScript};
chrome.contextMenus.create(Test2);

manifest.json

"background": {"scripts": ["background.js"]},
"web_accessible_resources": ["sc-dl.js"],
"permissions": ["contextMenus","tabs","http://*/*","https://*/*"],

This is the file you want to run from a "ContextMenu" or "browserAction"

1 Answers1

0

If you look at the code you're trying to inject, you'll see that it's not plain JS, and requires a whole bunch of other libraries.

You can inject them all first, but considering this was a bookmarklet, it was probably intended to be executed in a context of a certain page.

Chrome's content scripts live in an isolated context, so you should try injecting into the page's context instead. If it has all the libraries loaded already, it should work.

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206