content.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. (function() {
  2. console.log('Content script loaded');
  3. /**
  4. * Check if the current URL matches the pattern
  5. * @param {string} urlPattern - URL pattern to match
  6. * @returns {boolean} - Whether the current URL matches the pattern
  7. */
  8. function matchesUrlPattern(urlPattern) {
  9. const regex = new RegExp(urlPattern);
  10. return regex.test(window.location.href);
  11. }
  12. /**
  13. * Function to inject and execute custom code on the page
  14. */
  15. function injectCustomCode() {
  16. console.log('Custom code injected into the page');
  17. // Example: Add a banner to the top of the page
  18. const banner = document.createElement('div');
  19. banner.textContent = 'This page has been modified by My Firefox Extension';
  20. banner.style.cssText = 'position: fixed; top: 0; left: 0; width: 100%; background: #ffcc00; padding: 10px; text-align: center; z-index: 9999;';
  21. document.body.prepend(banner);
  22. // You can add more functionality here
  23. }
  24. // Listen for messages from the popup or background script
  25. browser.runtime.onMessage.addListener((message) => {
  26. if (message.command === 'injectCode') {
  27. injectCustomCode();
  28. return Promise.resolve({status: 'Injection completed'});
  29. }
  30. });
  31. // Check if we should automatically inject code based on URL
  32. if (matchesUrlPattern('example\\.com')) {
  33. injectCustomCode();
  34. }
  35. })();