Chrome扩展程序修改新标签页正文
Chrome extension modify newtab page body?
我有Chrome扩展名,它可以为页面正文添加新的div。但是当我尝试在新标签页面上运行它时,它不起作用。
这是我的背景.js:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null, {file: "commonAddWidget.js"};
});
我发现可以通过添加内容脚本并在单击浏览器上的扩展图标后向其发送消息来解决它。
背景.js:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function (tabs) {
var url = tabs[0].url;
if (url.indexOf('chrome://newtab') > -1) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {someObject: someValue});
});
}
}); });
内容脚本.js:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {// this code can modify NewTab page, and will modify only it });