Skip to content

Commit

Permalink
Add pipeline, Prettier config, and format code (#1)
Browse files Browse the repository at this point in the history
* Add pipeline, Prettier config, and format code

* Intentionally violate style

* Revert "Intentionally violate style"

This reverts commit 74a7234.
  • Loading branch information
jsh9 authored Apr 25, 2023
1 parent 6a7d2da commit 7edd78a
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 22 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: CI/CD pipeline

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
- run: |
npm install
git diff --exit-code
- name: Run code auto-formatting
run: |
npm run auto-format
git diff --exit-code
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "all",
"tabWidth": 2,
"semi": true,
"singleQuote": true
}
21 changes: 7 additions & 14 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ chrome.action.onClicked.addListener((tab) => {
console.log(`User-specified zoom levels: (${zoomLevel1}, ${zoomLevel2})`);

// Toggle between the two user-defined zoom levels
let newZoom = determineNewZoomLevel(
currentZoom,
zoomLevel1,
zoomLevel2,
);
let newZoom = determineNewZoomLevel(currentZoom, zoomLevel1, zoomLevel2);

// Apply the new zoom level to all open tabs
setZoomLevelForAllTabs(currentZoom, newZoom);
Expand All @@ -50,8 +46,8 @@ chrome.action.onClicked.addListener((tab) => {
// Function to set the zoom level for all open tabs
function setZoomLevelForAllTabs(currentZoom, targetZoom) {
console.log(
'>> Setting zoom level for all tabs from '
+ `${currentZoom} to ${targetZoom}`
'>> Setting zoom level for all tabs from ' +
`${currentZoom} to ${targetZoom}`,
);
chrome.tabs.query({}, (tabs) => {
tabs.forEach((tab) => {
Expand All @@ -64,19 +60,18 @@ function setZoomLevelForAllTabs(currentZoom, targetZoom) {

// Apply the stored zoom level to all new tabs when they are created
chrome.tabs.onCreated.addListener((tab) => {
console.log('------- New tab created -------')
console.log('------- New tab created -------');
setZoomToTargetLevelInGivenTab(tab.id);
});

// Apply the stored zoom level to all updated tabs when they are refreshed
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
console.log('------- Tab refreshed -------')
console.log('------- Tab refreshed -------');
if (changeInfo.status === 'complete') {
setZoomToTargetLevelInGivenTab(tabId);
}
});


function setZoomToTargetLevelInGivenTab(tabId) {
chrome.tabs.getZoom(tabId, (currentZoomRaw) => {
chrome.storage.sync.get(TARGET_ZOOM_LEVEL_KEY, (result) => {
Expand All @@ -85,20 +80,19 @@ function setZoomToTargetLevelInGivenTab(tabId) {

console.log('>> Setting zoom level for current tab');
console.log(
` Current zoom: ${currentZoom}; target zoom: ${targetZoom}`
` Current zoom: ${currentZoom}; target zoom: ${targetZoom}`,
);

if (areNumbersClose(currentZoom, targetZoom)) {
console.log('Two zoom levels are close enough; no actions');
} else {
console.log(` Setting current zoom level to ${targetZoom}`)
console.log(` Setting current zoom level to ${targetZoom}`);
chrome.tabs.setZoom(tabId, targetZoom / 100);
}
});
});
}


function determineNewZoomLevel(currentZoom, zoomLevel1, zoomLevel2) {
let newZoomLevel = areNumbersClose(currentZoom, zoomLevel1)
? zoomLevel2
Expand All @@ -107,7 +101,6 @@ function determineNewZoomLevel(currentZoom, zoomLevel1, zoomLevel2) {
return newZoomLevel;
}


function areNumbersClose(a, b) {
return Math.abs(a - b) < EPSILON;
}
19 changes: 11 additions & 8 deletions options.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ document.addEventListener('DOMContentLoaded', () => {
const zoom1 = parseInt(document.getElementById('zoom1').value);
const zoom2 = parseInt(document.getElementById('zoom2').value);

chrome.storage.sync.set({ [ZOOM_LEVEL_1_KEY]: zoom1, [ZOOM_LEVEL_2_KEY]: zoom2 }, () => {
// Display a message to the user to indicate that the zoom levels have been saved
const status = document.getElementById('status');
status.textContent = 'Zoom levels saved.';
setTimeout(() => {
status.textContent = '';
}, 2000);
});
chrome.storage.sync.set(
{ [ZOOM_LEVEL_1_KEY]: zoom1, [ZOOM_LEVEL_2_KEY]: zoom2 },
() => {
// Display a message to the user to indicate that the zoom levels have been saved
const status = document.getElementById('status');
status.textContent = 'Zoom levels saved.';
setTimeout(() => {
status.textContent = '';
}, 2000);
},
);
});
});
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "chrome-zoom-toggler",
"scripts": {
"auto-format": "npx prettier --write '*.js'"
},
"author": "https://github.com/jsh9",
"license": "MIT",
"devDependencies": {
"prettier": "2.8.8"
}
}

0 comments on commit 7edd78a

Please sign in to comment.