-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
63 lines (51 loc) · 2.59 KB
/
Copy pathcontent.js
File metadata and controls
63 lines (51 loc) · 2.59 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
// content.js
function getRouteDistance() {
const distanceElement = document.querySelector('.section-directions-trip-distance');
return distanceElement ? distanceElement.textContent : null;
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "getDistance") {
sendResponse({distance: getRouteDistance()});
}
});
// popup.js
document.addEventListener('DOMContentLoaded', function() {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {action: "getDistance"}, function(response) {
if (response && response.distance) {
document.getElementById('distanceInfo').textContent = `Route distance: ${response.distance}`;
}
});
});
document.getElementById('calculate').addEventListener('click', function() {
const height = document.getElementById('height').value;
const weight = document.getElementById('weight').value;
const gender = document.getElementById('gender').value;
const walkingType = document.getElementById('walkingType').value;
const distanceText = document.getElementById('distanceInfo').textContent;
const distance = parseFloat(distanceText.match(/\d+(\.\d+)?/)[0]);
const results = calculateStepsAndCalories(distance, height, weight, gender, walkingType);
document.getElementById('results').innerHTML = `
<p>Estimated Steps: ${results.steps}</p>
<p>Estimated Calories: ${results.calories}</p>
<p>Estimated Time: ${results.time} minutes</p>
`;
});
});
function calculateStepsAndCalories(distance, height, weight, gender, walkingType) {
let stepLength = (gender === 'male' ? 0.415 : 0.413) * (parseFloat(height) / 100);
const walkingFactors = {
'leisurely': { stepMod: 0.9, metsValue: 2.5, speed: 3 },
'moderate': { stepMod: 1.0, metsValue: 3.5, speed: 4.5 },
'brisk': { stepMod: 1.1, metsValue: 4.5, speed: 6 }
};
const factor = walkingFactors[walkingType];
stepLength *= factor.stepMod;
const distanceMeters = distance * 1000;
const estimatedSteps = Math.round(distanceMeters / stepLength);
const weightKg = parseFloat(weight);
const durationHours = distanceMeters / (factor.speed * 1000);
const caloriesBurned = Math.round(factor.metsValue * weightKg * durationHours);
const timeMinutes = Math.round((durationHours * 60) * 10) / 10;
return { steps: estimatedSteps, calories: caloriesBurned, time: timeMinutes };
}