chrome.loadTimes()
هي واجهة برمجة تطبيقات غير عادية تعرض مقاييس التحميل ومعلومات الشبكة للمطوّرين لمساعدتهم في فهم أداء مواقعهم الإلكترونية في الواقع بشكل أفضل.
منذ تنفيذ واجهة برمجة التطبيقات هذه في 2009، يمكن العثور على جميع المعلومات المفيدة التي تشير إليها في واجهات برمجة التطبيقات الموحّدة مثل:
- وقت التنقل 2
- Paint Timing
- الإضافة
nextHopProtocol
إلى Navigation Timing 2 وResource Timing 2.
ينفّذ العديد من مورّدي المتصفّحات هذه واجهات برمجة التطبيقات الموحّدة. ونتيجةً لذلك، سيتم إيقاف chrome.loadTimes()
نهائيًا في الإصدار 64 من Chrome.
واجهة برمجة التطبيقات التي تم إيقافها
تعرض الدالة chrome.loadTimes()
كائنًا واحدًا يحتوي على جميع معلومات التحميل والشبكة. على سبيل المثال، الكائن التالي هو نتيجة استدعاء chrome.loadTimes()
على www.google.com:
{
"requestTime": 1513186741.847,
"startLoadTime": 1513186741.847,
"commitLoadTime": 1513186742.637,
"finishDocumentLoadTime": 1513186742.842,
"finishLoadTime": 1513186743.582,
"firstPaintTime": 1513186742.829,
"firstPaintAfterLoadTime": 0,
"navigationType": "Reload",
"wasFetchedViaSpdy": true,
"wasNpnNegotiated": true,
"npnNegotiatedProtocol": "h2",
"wasAlternateProtocolAvailable": false,
"connectionInfo": "h2"
}
عمليات الاستبدال الموحّدة
يمكنك الآن العثور على كل من القيم السابقة باستخدام واجهات برمجة التطبيقات الموحّدة. يتطابق الجدول التالي مع كل قيمة لواجهة برمجة التطبيقات الموحّدة، وتعرض الأقسام أدناه أمثلة على الرموز البرمجية حول كيفية الحصول على كل قيمة في واجهة برمجة التطبيقات القديمة باستخدام مكافئات حديثة.
ميزة واحدة (chrome.loadTimes() )
| الاستبدال الموحّد لواجهة برمجة التطبيقات |
---|---|
requestTime |
وقت التنقّل 2 |
startLoadTime |
وقت التنقّل 2 |
commitLoadTime |
وقت التنقّل 2 |
finishDocumentLoadTime |
وقت التنقّل 2 |
finishLoadTime |
وقت التنقّل 2 |
firstPaintTime |
مدة عرض محتوى الصفحة |
firstPaintAfterLoadTime |
لا ينطبق |
navigationType |
وقت التنقّل 2 |
wasFetchedViaSpdy |
وقت التنقّل 2 |
wasNpnNegotiated |
وقت التنقّل 2 |
npnNegotiatedProtocol |
وقت التنقّل 2 |
wasAlternateProtocolAvailable |
لا ينطبق |
connectionInfo |
وقت التنقّل 2 |
تعرض أمثلة الرموز أدناه قيمًا مكافئة للقيم التي يعرضها
chrome.loadTimes()
. ومع ذلك، بالنسبة إلى التعليمات البرمجية الجديدة، لا يوصى
بأمثلة التعليمات البرمجية هذه. السبب هو أنّ السمة chrome.loadTimes()
تقدّم قيمًا للأوقات في وقت الحقبة بالثواني، بينما تُعِدّ واجهات برمجة التطبيقات الجديدة للأداء عادةً القيم بالملي ثانية بالنسبة إلى أصل وقت الصفحة، ما قد يكون أكثر فائدة لتحليل الأداء.
يفضّل العديد من الأمثلة أيضًا واجهات برمجة تطبيقات الأداء الزمني 2 لواجهات برمجة التطبيقات (مثل
performance.getEntriesByType()
)، لكنّها توفّر عناصر احتياطية لواجهة برمجة التطبيقات
Navigation Timing 1 القديمة لأنّها تتيح استخدام
المتصفحات على نطاق أوسع. ومن الآن فصاعدًا، يتم تفضيل واجهات برمجة تطبيقات المخطط الزمني للأداء
وعادةً ما يتم الإبلاغ عنها بدقة أعلى.
requestTime
function requestTime() {
// If the browser supports the Navigation Timing 2 and HR Time APIs, use
// them, otherwise fall back to the Navigation Timing 1 API.
if (window.PerformanceNavigationTiming && performance.timeOrigin) {
const ntEntry = performance.getEntriesByType('navigation')[0];
return (ntEntry.startTime + performance.timeOrigin) / 1000;
} else {
return performance.timing.navigationStart / 1000;
}
}
startLoadTime
function startLoadTime() {
// If the browser supports the Navigation Timing 2 and HR Time APIs, use
// them, otherwise fall back to the Navigation Timing 1 API.
if (window.PerformanceNavigationTiming && performance.timeOrigin) {
const ntEntry = performance.getEntriesByType('navigation')[0];
return (ntEntry.startTime + performance.timeOrigin) / 1000;
} else {
return performance.timing.navigationStart / 1000;
}
}
commitLoadTime
function commitLoadTime() {
// If the browser supports the Navigation Timing 2 and HR Time APIs, use
// them, otherwise fall back to the Navigation Timing 1 API.
if (window.PerformanceNavigationTiming && performance.timeOrigin) {
const ntEntry = performance.getEntriesByType('navigation')[0];
return (ntEntry.responseStart + performance.timeOrigin) / 1000;
} else {
return performance.timing.responseStart / 1000;
}
}
finishDocumentLoadTime
function finishDocumentLoadTime() {
// If the browser supports the Navigation Timing 2 and HR Time APIs, use
// them, otherwise fall back to the Navigation Timing 1 API.
if (window.PerformanceNavigationTiming && performance.timeOrigin) {
const ntEntry = performance.getEntriesByType('navigation')[0];
return (ntEntry.domContentLoadedEventEnd + performance.timeOrigin) / 1000;
} else {
return performance.timing.domContentLoadedEventEnd / 1000;
}
}
finishLoadTime
function finishLoadTime() {
// If the browser supports the Navigation Timing 2 and HR Time APIs, use
// them, otherwise fall back to the Navigation Timing 1 API.
if (window.PerformanceNavigationTiming && performance.timeOrigin) {
const ntEntry = performance.getEntriesByType('navigation')[0];
return (ntEntry.loadEventEnd + performance.timeOrigin) / 1000;
} else {
return performance.timing.loadEventEnd / 1000;
}
}
firstPaintTime
function firstPaintTime() {
if (window.PerformancePaintTiming) {
const fpEntry = performance.getEntriesByType('paint')[0];
return (fpEntry.startTime + performance.timeOrigin) / 1000;
}
}
firstPaintAfterLoadTime
function firstPaintTimeAfterLoad() {
// This was never actually implemented and always returns 0.
return 0;
}
navigationType
function navigationType() {
if (window.PerformanceNavigationTiming) {
const ntEntry = performance.getEntriesByType('navigation')[0];
return ntEntry.type;
}
}
wasFetchedViaSpdy
function wasFetchedViaSpdy() {
// SPDY is deprecated in favor of HTTP/2, but this implementation returns
// true for HTTP/2 or HTTP2+QUIC/39 as well.
if (window.PerformanceNavigationTiming) {
const ntEntry = performance.getEntriesByType('navigation')[0];
return ['h2', 'hq'].includes(ntEntry.nextHopProtocol);
}
}
wasNpnNegotiated
function wasNpnNegotiated() {
// NPN is deprecated in favor of ALPN, but this implementation returns true
// for HTTP/2 or HTTP2+QUIC/39 requests negotiated via ALPN.
if (window.PerformanceNavigationTiming) {
const ntEntry = performance.getEntriesByType('navigation')[0];
return ['h2', 'hq'].includes(ntEntry.nextHopProtocol);
}
}
npnNegotiatedProtocol
function npnNegotiatedProtocol() {
// NPN is deprecated in favor of ALPN, but this implementation returns the
// HTTP/2 or HTTP2+QUIC/39 requests negotiated via ALPN.
if (window.PerformanceNavigationTiming) {
const ntEntry = performance.getEntriesByType('navigation')[0];
return ['h2', 'hq'].includes(ntEntry.nextHopProtocol) ?
ntEntry.nextHopProtocol : 'unknown';
}
}
wasAlternateProtocolAvailable
function wasAlternateProtocolAvailable() {
// The Alternate-Protocol header is deprecated in favor of Alt-Svc
// (https://www.mnot.net/blog/2016/03/09/alt-svc), so technically this
// should always return false.
return false;
}
connectionInfo
function connectionInfo() {
if (window.PerformanceNavigationTiming) {
const ntEntry = performance.getEntriesByType('navigation')[0];
return ntEntry.nextHopProtocol;
}
}
خطة الإزالة
سيتم إيقاف واجهة برمجة التطبيقات chrome.loadTimes()
API في الإصدار 64 من Chrome نهائيًا ومن المقرر إزالتها في أواخر عام 2018. وعلى مطوّري البرامج نقل الرموز في أقرب وقت ممكن
لتجنُّب أي فقدان في البيانات.