Я пытаюсь выяснить, как работают скрипты содержимого.Почему Chrome игнорирует мою команду executeScript в моем расширении?
Я пишу расширение с контекстным меню, которое после нажатия на кнопку следующего щелчка приведет к диалогу оповещений с информацией о странице, на которой находится элемент, и имени и имени элемента.
Я думаю, что моя проблема заключается в том, что имя файла сценария содержимого находится где-то на моем манифесте, так как я использую executeScript
, чтобы использовать его вместо того, чтобы его вводили на каждую веб-страницу, но я не могу понять, куда его поместить.
Пожалуйста, дайте мне знать, если я могу предоставить более полезную информацию.
Вот копии моего манифеста, основной скрипт, который я использую, и сценарий, который я пытаюсь вставить.
manifest.json
{
"name": "Omnixx Page Object Builder",
"description": "Finds information about an element clicked, started with a context menu",
"version": "0.3",
"permissions": ["tabs", "contextMenus", "activeTab"],
"background": {
"persistent": false,
"scripts": ["menuBuilder.js"]
},
"manifest_version": 2
}
menuBuilder.js
function onClickHandler(info, tab) {
if (info.menuItemId == "start") {
console.log("Started trying to find elements");
chrome.tabs.executeScript(null, {file:"elementTracker.js"});
console.log("Started listening...");
}
};
chrome.contextMenus.onClicked.addListener(onClickHandler);
chrome.runtime.onInstalled.addListener(function() {
chrome.contextMenus.create({"title": "Find element information", "id": "start", "contexts":["all", "page", "frame", "selection", "link", "editable", "image","video", "audio"], });
});
elementTracker.js
function click(event) {
// Begin building the message
var msgToWrite = "";
var curscreen = window.HIDEFRAME.document.getElementsByName("curscreen")[1].value;
msgToWrite += "curscreen: '" + curscreen + "'\n";
var url = content.document.URL;
msgToWrite += "URL: " + url + "\n";
// A try-catch block is necessary for finding the frame; otherwise the code
// will fail for pages without frames
try {
var frame = event.originalTarget.ownerDocument.defaultView.frameElement;
var frameName = frame.getAttribute("name");
if (frameName != null) {
msgToWrite += "Frame: '" + frameName + "'\n";
} else {
msgToWrite += "No frame found\n";
}
} catch (e) {
msgToWrite += "No frame found\n";
}
// Get the element's ID
var elemID = event.target.getAttribute("id");
if (elemID != null) {
msgToWrite += "ID: '" + elemID + "'\n";
} else {
msgToWrite += "No ID found\n";
}
// Get the element's name
var elemName = event.target.getAttribute("name");
if (elemName != null) {
msgToWrite += "Name: '" + elemName + "'";
} else {
msgToWrite += "No name found";
}
// Create a pop-up message
alert(msgToWrite);
}
document.addEventListener('click', click);
Я отредактировал ваш вопрос, пожалуйста, избегайте «стены текста», это верный способ не получить ответ здесь в переполнении стека. Добро пожаловать, и удачи. Вероятно, вы должны уточнить свой вопрос, поскольку раньше я разрабатывал хромовые расширения, и я все еще не уверен, что вы здесь задаете. –