I'm currently working on a project involving a Chrome Extension that aims to enable disabled drop-down menus. Strangely, when running the script in the Console, everything works flawlessly. However, when executing it through the extension, the functions are called but nothing happens.
Your assistance would be greatly appreciated.
manifest.json
{
"name": "4x6 Mac Menu",
"manifest_version": 2
"version": "0.1.4",
"description": "Overrides Disabled Drop Down",
"permissions": ["contextMenus"],
"background": {
"scripts": ["sample-click.js"]
}
}
sample-click.js
function Enable46(info, tab) {
console.log("Enable 4x6 was clicked.");
var inputs = document.getElementsByClassName('psSelect');
for(var i = 0; i < inputs.length; i++) {
inputs[i].disabled = false;
}
console.log("Drop Enabled.");
}
chrome.contextMenus.create({
title: "Enable 4x6",
contexts: ["page"],
onclick: Enable46,
});
Another attempt involved adding a listener as background. Although the console log is displayed, the event fails to carry out the intended function.
manifest.json
{
"name": "4x6 Enable",
"description": "Enable 4x6 Print Option on Mac",
"version": "0.1",
"manifest_version": 2,
"permissions": [
"contextMenus",
"activeTab"
],
"background": {
"persistent": false,
"scripts": ["bg.js"]
}
}
bg.js
/* Create a context-menu */
chrome.contextMenus.create({
id: "myContextMenu",
title: "4x6 Label",
contexts: ["all"]
});
/* Register a listener for the `onClicked` event */
chrome.contextMenus.onClicked.addListener(function(info, tab) {
if (tab) {
/* Create the code to be injected */
var code = [
'var input = document.getElementsByClassName("psSelect");',
'for(var i = 0; i < inputs.length; i++) { inputs[i].disabled = false; }'
].join("\n");
console.log("Enable 4x6 was clicked.");
/* Inject the code into the current tab */
chrome.tabs.executeScript(tab.id, { code: code });
}
});