popup.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Function to display status messages
  2. function showStatus(message) {
  3. document.getElementById('status').textContent = message;
  4. setTimeout(() => {
  5. document.getElementById('status').textContent = '';
  6. }, 3000);
  7. }
  8. // Function to inject script into the current tab
  9. function injectScript() {
  10. browser.tabs.query({active: true, currentWindow: true})
  11. .then(tabs => {
  12. browser.tabs.sendMessage(tabs[0].id, { command: 'injectCode' })
  13. .then(response => {
  14. showStatus('Script injected successfully!');
  15. })
  16. .catch(error => {
  17. showStatus('Error: Content script may not be loaded. Try refreshing the page.');
  18. // Fallback: Directly inject the content script
  19. browser.tabs.executeScript(tabs[0].id, {
  20. file: "/content_scripts/content.js"
  21. });
  22. });
  23. });
  24. }
  25. // Function to navigate to another page
  26. function navigateTab() {
  27. const url = document.getElementById('navigation-url').value;
  28. if (!url) {
  29. showStatus('Please enter a URL');
  30. return;
  31. }
  32. // Add http:// prefix if missing
  33. const formattedUrl = url.includes('://') ? url : `http://${url}`;
  34. browser.tabs.query({active: true, currentWindow: true})
  35. .then(tabs => {
  36. browser.runtime.sendMessage({
  37. command: 'navigate',
  38. tabId: tabs[0].id,
  39. url: formattedUrl
  40. })
  41. .then(response => {
  42. if (response && response.status === 'success') {
  43. showStatus('Tab navigated successfully!');
  44. } else {
  45. showStatus('Error navigating tab: ' + (response ? response.message : 'Unknown error'));
  46. }
  47. })
  48. .catch(error => {
  49. showStatus('Error navigating tab: ' + error.message);
  50. });
  51. });
  52. }
  53. // Function to save data to storage
  54. function saveData() {
  55. const key = document.getElementById('storage-key').value;
  56. const value = document.getElementById('storage-value').value;
  57. if (!key) {
  58. showStatus('Please enter a key');
  59. return;
  60. }
  61. browser.storage.local.set({ [key]: value })
  62. .then(() => {
  63. document.getElementById('storage-result').textContent = `Data saved: ${key} = ${value}`;
  64. })
  65. .catch(error => {
  66. document.getElementById('storage-result').textContent = 'Error saving data: ' + error.message;
  67. });
  68. }
  69. // Function to load data from storage
  70. function loadData() {
  71. const key = document.getElementById('storage-key').value;
  72. if (!key) {
  73. showStatus('Please enter a key');
  74. return;
  75. }
  76. browser.storage.local.get(key)
  77. .then(result => {
  78. if (result[key] !== undefined) {
  79. document.getElementById('storage-value').value = result[key];
  80. document.getElementById('storage-result').textContent = `Data loaded: ${key} = ${result[key]}`;
  81. } else {
  82. document.getElementById('storage-result').textContent = `No data found for key: ${key}`;
  83. }
  84. })
  85. .catch(error => {
  86. document.getElementById('storage-result').textContent = 'Error loading data: ' + error.message;
  87. });
  88. }
  89. // Add event listeners when the document is loaded
  90. document.addEventListener('DOMContentLoaded', function() {
  91. document.getElementById('inject-button').addEventListener('click', injectScript);
  92. document.getElementById('navigate-button').addEventListener('click', navigateTab);
  93. document.getElementById('save-data').addEventListener('click', saveData);
  94. document.getElementById('load-data').addEventListener('click', loadData);
  95. // Example of using storage to track last usage
  96. browser.storage.local.get('lastUsed')
  97. .then(result => {
  98. if (result.lastUsed) {
  99. showStatus('Last used: ' + new Date(result.lastUsed).toLocaleString());
  100. }
  101. browser.storage.local.set({ 'lastUsed': Date.now() });
  102. });
  103. });