البرمجة

تغيير قيمة التاريخ والوقت في متصفح Chrome

To change the browser’s Date/time value using a Chrome extension, you can use the chrome.storage API to store the user’s preferred timezone offset. Then, whenever new Date() is called in your extension’s content scripts, you can adjust the returned value based on the stored offset.

Here’s a basic example of how you might achieve this:

  1. Create a manifest file (manifest.json) for your extension:
json
{ "manifest_version": 2, "name": "Timezone Changer", "version": "1.0", "permissions": ["storage"], "content_scripts": [ { "matches": [""], "js": ["content.js"] } ] }
  1. Create a JavaScript file (content.js) for your content script:
javascript
// Load the stored timezone offset or use the default (0) chrome.storage.sync.get("timezoneOffset", function(data) { const timezoneOffset = data.timezoneOffset || 0; // Override the Date constructor to adjust the returned value const oldDate = Date; window.Date = function(...args) { if (args.length === 0) { return new oldDate(Date.now() + timezoneOffset); } else { return new oldDate(...args); } }; }); // Listen for messages from the extension popup chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { if (request.action === "setTimezoneOffset") { // Save the user's timezone offset preference chrome.storage.sync.set({ "timezoneOffset": request.timezoneOffset }); } });
  1. Create an HTML file (popup.html) for your extension’s popup:
html
html> <html> <head> <title>Timezone Changertitle> <script src="popup.js">script> head> <body> <h1>Timezone Changerh1> <label for="timezone">Timezone Offset (in minutes):label> <input type="number" id="timezone" /> <button id="save">Savebutton> body> html>
  1. Create a JavaScript file (popup.js) for your popup script:
javascript
document.addEventListener("DOMContentLoaded", function() { const timezoneInput = document.getElementById("timezone"); const saveButton = document.getElementById("save"); // Load the stored timezone offset and update the input value chrome.storage.sync.get("timezoneOffset", function(data) { timezoneInput.value = data.timezoneOffset || 0; }); // Save the timezone offset when the save button is clicked saveButton.addEventListener("click", function() { const timezoneOffset = parseInt(timezoneInput.value, 10); chrome.runtime.sendMessage({ action: "setTimezoneOffset", timezoneOffset: timezoneOffset }); }); });

With this setup, your extension will override the Date constructor to adjust the returned value based on the stored timezone offset. Users can set their preferred timezone offset using the extension’s popup, and the offset will be applied to all new Date() calls in the browser.

Make sure to test your extension thoroughly, as changing the behavior of core JavaScript functions like Date can have unintended consequences on web pages.

المزيد من المعلومات

لتحقيق هذا الهدف، يمكنك استخدام تطبيق Chrome Extension للسماح للمستخدم بتعيين تفضيلاته للمنطقة الزمنية ومن ثم تعديل قيمة الوقت المُسترجعة من new Date() بناءً على هذه التفضيلات. يمكنك القيام بذلك عن طريق تنفيذ الخطوات التالية:

  1. إنشاء ملف التعريف (manifest.json):
json
{ "manifest_version": 2, "name": "Timezone Changer", "version": "1.0", "permissions": ["storage"], "content_scripts": [ { "matches": [""], "js": ["content.js"] } ], "browser_action": { "default_popup": "popup.html" }, "background": { "scripts": ["background.js"], "persistent": false } }
  1. إنشاء ملف لسكربت الصفحة (content.js):
javascript
// تحميل تفضيلات المنطقة الزمنية المحفوظة أو استخدام القيمة الافتراضية (0) chrome.storage.sync.get("timezoneOffset", function(data) { const timezoneOffset = data.timezoneOffset || 0; // تعديل قيمة الوقت المُسترجعة من new Date() بناءً على الفارق في التوقيت const oldDate = Date; window.Date = function(...args) { if (args.length === 0) { return new oldDate(Date.now() + timezoneOffset); } else { return new oldDate(...args); } }; });
  1. إنشاء ملف HTML للنافذة المنبثقة (popup.html):
html
html> <html> <head> <title>Timezone Changertitle> <script src="popup.js">script> head> <body> <h1>Timezone Changerh1> <label for="timezone">Timezone Offset (in minutes):label> <input type="number" id="timezone" /> <button id="save">Savebutton> body> html>
  1. إنشاء ملف لسكربت النافذة المنبثقة (popup.js):
javascript
document.addEventListener("DOMContentLoaded", function() { const timezoneInput = document.getElementById("timezone"); const saveButton = document.getElementById("save"); // تحميل تفضيلات المنطقة الزمنية المحفوظة وتحديث قيمة الإدخال chrome.storage.sync.get("timezoneOffset", function(data) { timezoneInput.value = data.timezoneOffset || 0; }); // حفظ تفضيلات المنطقة الزمنية عند النقر على زر الحفظ saveButton.addEventListener("click", function() { const timezoneOffset = parseInt(timezoneInput.value, 10); chrome.storage.sync.set({ "timezoneOffset": timezoneOffset }); }); });
  1. إنشاء ملف للخلفية (background.js):
javascript
// إعادة تعيين تفضيلات المنطقة الزمنية إلى القيمة الافتراضية عند بدء تشغيل الامتداد chrome.runtime.onInstalled.addListener(function() { chrome.storage.sync.set({ "timezoneOffset": 0 }); });

من خلال تنفيذ هذه الخطوات، سيكون بإمكان المستخدم تعيين تفضيلاته للمنطقة الزمنية من خلال النافذة المنبثقة، وسيتم تعديل قيمة الوقت المُسترجعة من new Date() بناءً على هذه التفضيلات.

مقالات ذات صلة

زر الذهاب إلى الأعلى

هذا المحتوى محمي من النسخ لمشاركته يرجى استعمال أزرار المشاركة السريعة أو تسخ الرابط !!