-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
115 lines (98 loc) · 3.62 KB
/
content.js
File metadata and controls
115 lines (98 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Content script that executes when pages load/reload
console.log('Content script loaded on:', window.location.href);
// Function to execute your custom JavaScript
function executeOnPageLoad() {
console.log('Page loaded/reloaded at:', new Date().toISOString());
console.log('Current URL:', window.location.href);
console.log('Document ready state:', document.readyState);
// Check if JavaScript is being blocked
checkJavaScriptBlocking();
// Add your custom JavaScript code here
// Example: Change page background color
// document.body.style.backgroundColor = '#f0f8ff';
// Example: Add a notification banner
// addNotificationBanner();
// Example: Log all links on the page
// logAllLinks();
// Send message to background script
chrome.runtime.sendMessage({
action: 'pageLoaded',
url: window.location.href,
timestamp: Date.now()
});
}
// Function to check if JavaScript is being blocked
function checkJavaScriptBlocking() {
const scriptTags = document.querySelectorAll('script[src]');
const inlineScripts = document.querySelectorAll('script:not([src])');
console.log(`Found ${scriptTags.length} external scripts and ${inlineScripts.length} inline scripts`);
// Attach error event listeners to scripts to detect loading errors
scriptTags.forEach(script => {
if (!script.dataset.errorListenerAttached) {
script.addEventListener('error', () => {
script.dataset.loadError = 'true';
});
script.dataset.errorListenerAttached = 'true';
}
});
// Check if any external scripts failed to load (by looking for scripts with error attribute)
scriptTags.forEach(script => {
console.log('Script:', script.src, ' reload');
try{
// Attempt to reload the script
const newScript = document.createElement('script');
newScript.src = script.src;
// Optionally, copy attributes except for dataset
Array.from(script.attributes).forEach(attr => {
if (attr.name !== 'src') newScript.setAttribute(attr.name, attr.value);
});
// Replace the old script with the new one
script.parentNode.replaceChild(newScript, script);
console.log(`Attempted to reload blocked script: ${script.src}`);
} catch(e){
console.error('Error reloading script:', e);
}
});
setTimeout(() => {
let blocked = false;
scriptTags.forEach(script => {
if (script.dataset.loadError === 'true') {
blocked = true;
}
});
if (blocked) {
console.log('⚠️ Some scripts failed to load - may be blocked');
} else {
console.log('✅ No obvious script blocking detected');
}
}, 3000);
}
// Function to log all links (example)
function logAllLinks() {
const links = document.querySelectorAll('a[href]');
console.log(`Found ${links.length} links on the page:`);
links.forEach((link, index) => {
console.log(`${index + 1}. ${link.href} - "${link.textContent.trim()}"`);
});
}
// Execute immediately when script loads
executeOnPageLoad();
// Listen for messages from popup or background script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log('Message received:', request);
if (request.action === 'executeScript') {
console.log('Received message to execute script');
executeOnPageLoad();
sendResponse({ success: true });
}
if (request.action === 'getPageInfo') {
console.log('Action:', request.action);
checkJavaScriptBlocking();
sendResponse({
url: window.location.href,
title: document.title,
links: document.querySelectorAll('a').length,
images: document.querySelectorAll('img').length
});
}
});