inject script

This commit is contained in:
go0p
2024-05-15 14:43:04 +08:00
committed by go0p
parent f6cf369b3a
commit 8cdd5605bc
4 changed files with 36 additions and 4 deletions
+13 -3
View File
@@ -52,8 +52,18 @@ chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
}
})
chrome.tabs.query({}, (tabs) => {
tabs.forEach(function (tab) {
console.log("Tab ID",tab.id); // 输出每个标签页的ID
chrome.runtime.onInstalled.addListener(() => {
chrome.scripting.registerContentScripts([{
id: 'myScript',
matches: ['<all_urls>'], // 根据需要调整匹配模式
js: ['inject.js'],
runAt: 'document_start'
}], (result) => {
if (chrome.runtime.lastError) {
console.error(`Error registering script: ${chrome.runtime.lastError.message}`);
} else {
console.log('Script registered', result);
}
});
});
+5
View File
@@ -83,6 +83,11 @@ export class WebSocketManager {
getTabId().then((tabId) => {
// 确保tabId和code有效
if (typeof tabId === 'number' && typeof code === 'string') {
chrome.webNavigation.getAllFrames({tabId: tabId}, function(frames) {
for (let frame of frames) {
console.log(`Frame ID: ${frame.frameId} with URL: ${frame.url}`);
}
});
// 在指定的tabId上执行脚本
chrome.scripting.executeScript({
target: {tabId: tabId},
+13
View File
@@ -0,0 +1,13 @@
console.log('This runs at document start, before any document content is parsed.');
// 保存原始的console.log函数的引用
const originalConsoleLog = console.log;
// 重写console.log
console.log = function (message) {
// 调用原始的console.log函数
originalConsoleLog("trying to open " + message + " window.");
};
window.open = function (url) {
console.log("trying to open " + url + " window.");
}
+5 -1
View File
@@ -16,6 +16,7 @@
"type": "module"
},
"permissions": [
"webNavigation",
"activeTab",
"scripting",
"tabs",
@@ -23,13 +24,16 @@
"storage",
"webRequest"
],
"host_permissions": [
"<all_urls>"
],
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"content.js"
"inject.js"
]
}
],