Skip to content

Commit

Permalink
Add improved browser detection
Browse files Browse the repository at this point in the history
The goal is to move away from browser detection for features. Specific browser will be used only for things like advertisement or special rendering.

Feature differentiation should be based on API availability as much as possible instead.

This could create problems if someone tries to manually install the wrong version of the extension or if they install it from the wrong store (Ex: install the extension from chrome web store on opera).
  • Loading branch information
Moustachauve committed Aug 20, 2023
1 parent 7b9db6e commit fc1cbb7
Show file tree
Hide file tree
Showing 16 changed files with 1,170 additions and 593 deletions.
41 changes: 40 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,44 @@ module.exports = function(grunt) {
},

},
replace: {
options: {
patterns: [
{
match: 'browser_name',
replacement: '<%= grunt.task.current.target %>'
}
]
},
firefox: {
files: [
{
expand: true, flatten: true, src: ['interface/lib/env.js'], dest: 'build/<%= grunt.task.current.target %>/interface/lib/'
}
]
},
chrome: {
files: [
{
expand: true, flatten: true, src: ['interface/lib/env.js'], dest: 'build/<%= grunt.task.current.target %>/interface/lib/'
}
]
},
edge: {
files: [
{
expand: true, flatten: true, src: ['interface/lib/env.js'], dest: 'build/<%= grunt.task.current.target %>/interface/lib/'
}
]
},
opera: {
files: [
{
expand: true, flatten: true, src: ['interface/lib/env.js'], dest: 'build/<%= grunt.task.current.target %>/interface/lib/'
}
]
}
},
removelogging: {
dist: {
src: "build/**/*.js"
Expand Down Expand Up @@ -159,10 +197,11 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-replace');
grunt.loadNpmTasks("grunt-remove-logging");
grunt.loadNpmTasks('grunt-contrib-compress');

// Default task(s).
grunt.registerTask('default', ['json-replace', 'jshint', 'clean', 'copy', 'removelogging', 'compress']);
grunt.registerTask('default', ['json-replace', 'jshint', 'clean', 'copy', 'replace', 'removelogging', 'compress']);

};
7 changes: 7 additions & 0 deletions cookie-editor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
if (typeof importScripts === 'function') {
importScripts('interface/lib/env.js');
importScripts('interface/lib/browserDetector.js');
}

Expand Down Expand Up @@ -33,6 +34,12 @@ if (typeof importScripts === 'function') {
}
});

if (browserDetector.supportsSidePanel()) {
browserDetector.getApi().sidePanel
.setPanelBehavior({ openPanelOnActionClick: false })
.catch((error) => console.error(error));
}

function handleMessage(request, sender, sendResponse) {
console.log('message received: ' + (request.type || 'unknown'));
switch (request.type) {
Expand Down
1 change: 1 addition & 0 deletions interface/devtools/cookie-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ <h1 class="container">Cookie Editor</h1>

<script src="../lib/animate.js"></script>
<script src="../lib/event.js"></script>
<script src="../lib/env.js"></script>
<script src="../lib/browserDetector.js"></script>
<script src="../lib/cookie.js"></script>
<script src="../lib/genericCookieHandler.js"></script>
Expand Down
1 change: 1 addition & 0 deletions interface/devtools/devtool.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<html>
<head>
<meta charset="utf-8">
<script src="../lib/env.js"></script>
<script src="../lib/browserDetector.js"></script>
<script src="devtools.js"></script>
</head>
Expand Down
63 changes: 33 additions & 30 deletions interface/lib/browserDetector.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,42 @@
function BrowserDetector() {
'use strict';
const env = new Env();
let namespace = chrome || window.browser || window.chrome;
let browserName;
let doesSupportSameSiteCookie = null;
let isIos = false;
let supportPromises = false;

try {
supportPromises = namespace.runtime.getPlatformInfo() instanceof Promise;
}
catch (e) {
}

if (namespace === chrome || namespace === window.chrome) {
if (supportPromises) {
browserName = 'safari';
}
else {
browserName = 'chrome';
}
} else if (namespace === window.browser) {
if (supportPromises) {
browserName = 'firefox';
}
else {
browserName = 'edge';
}
}

console.log(browserName);
let supportSidePanel = false;

this.getApi = function () {
return namespace;
};

this.isFirefox = function () {
return browserName === 'firefox';
return env.browserName === 'firefox';
};

this.isChrome = function () {
return browserName === 'chrome';
return env.browserName === 'chrome';
};

this.isEdge = function () {
return browserName === 'edge';
return env.browserName === 'edge';
};

this.isSafari = function () {
return browserName === 'safari';
return env.browserName === 'safari';
};

this.supportsPromises = function () {
return this.supportPromises;
}

this.supportsSidePanel = function () {
return this.supportSidePanel;
}

this.getBrowserName = function () {
return browserName;
return env.browserName;
}

this.supportSameSiteCookie = function () {
Expand Down Expand Up @@ -96,6 +77,28 @@ function BrowserDetector() {
return doesSupportSameSiteCookie;
}

try {
supportPromises = namespace.runtime.getPlatformInfo() instanceof Promise;
console.info('Promises support: ', supportPromises);
}
catch (e) {
}

try {
supportSidePanel = typeof this.getApi().sidePanel !== "undefined";
console.info('SidePanel support: ', supportSidePanel);
}
catch (e) {
}

if (env.browserName === "@@browser_name") {
env.browserName = "chrome";
console.warn("undefined browser name, using chrome as fallback");
}

console.log(env.browserName);


// We call it right away to make sure the value of doesSupportSameSiteCookie is initialized
this.supportSameSiteCookie();
}
3 changes: 3 additions & 0 deletions interface/lib/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function Env() {
this.browserName = "@@browser_name";
}
1 change: 1 addition & 0 deletions interface/popup-android/cookie-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ <h2></h2>

<script src="../lib/animate.js"></script>
<script src="../lib/event.js"></script>
<script src="../lib/env.js"></script>
<script src="../lib/browserDetector.js"></script>
<script src="../lib/cookie.js"></script>
<script src="../lib/genericCookieHandler.js"></script>
Expand Down
1 change: 1 addition & 0 deletions interface/popup-ios/cookie-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ <h2></h2>

<script src="../lib/animate.js"></script>
<script src="../lib/event.js"></script>
<script src="../lib/env.js"></script>
<script src="../lib/browserDetector.js"></script>
<script src="../lib/cookie.js"></script>
<script src="../lib/genericCookieHandler.js"></script>
Expand Down
1 change: 1 addition & 0 deletions interface/popup/cookie-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ <h1 class="container">Cookie Editor</h1>
<script src="../lib/isPopup.js"></script>
<script src="../lib/animate.js"></script>
<script src="../lib/event.js"></script>
<script src="../lib/env.js"></script>
<script src="../lib/browserDetector.js"></script>
<script src="../lib/cookie.js"></script>
<script src="../lib/genericCookieHandler.js"></script>
Expand Down
8 changes: 6 additions & 2 deletions manifest.chrome.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "Cookie-Editor",
"version": "1.11.0",
"version": "1.12.0",
"author": "Moustachauve",
"description": "Simple yet powerful Cookie Editor that allow you to quickly create, edit and delete cookies without leaving your tab.",
"offline_enabled": true,
Expand All @@ -19,11 +19,15 @@
"default_title": "Cookie-Editor",
"default_popup": "interface/popup/cookie-list.html"
},
"side_panel": {
"default_path": "interface/popup/cookie-list.html"
},
"devtools_page": "interface/devtools/devtool.html",
"permissions": [
"cookies",
"tabs",
"storage"
"storage",
"sidePanel"
],
"host_permissions": [
"<all_urls>"
Expand Down
8 changes: 6 additions & 2 deletions manifest.edge.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "Cookie-Editor",
"version": "1.11.0",
"version": "1.12.0",
"author": "Moustachauve",
"description": "Simple yet powerful Cookie Editor that allow you to quickly create, edit and delete cookies without leaving your tab.",
"offline_enabled": true,
Expand All @@ -18,11 +18,15 @@
"default_title": "Cookie-Editor",
"default_popup": "interface/popup/cookie-list.html"
},
"side_panel": {
"default_path": "interface/popup/cookie-list.html"
},
"devtools_page": "interface/devtools/devtool.html",
"permissions": [
"cookies",
"tabs",
"storage"
"storage",
"sidePanel"
],
"host_permissions": [
"<all_urls>"
Expand Down
10 changes: 9 additions & 1 deletion manifest.firefox.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "Cookie-Editor",
"version": "1.11.0",
"version": "1.12.0",
"description": "Simple yet powerful Cookie Editor that allow you to quickly create, edit and delete cookies without leaving your tab.",
"background": {
"scripts": [
Expand Down Expand Up @@ -36,6 +36,14 @@
"default_title": "Cookie-Editor",
"default_popup": "interface/popup/cookie-list.html"
},
"sidebar_action": {
"default_icon": {
"16": "icons/cookie-filled-small.svg",
"32": "icons/cookie-filled-small.svg"
},
"default_title": "Cookie-Editor",
"default_panel": "interface/popup/cookie-list.html"
},
"devtools_page": "interface/devtools/devtool.html",
"permissions": [
"cookies",
Expand Down
2 changes: 1 addition & 1 deletion manifest.opera.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "Cookie-Editor",
"version": "1.11.0",
"version": "1.12.0",
"author": "Moustachauve",
"description": "Simple yet powerful Cookie Editor that allow you to quickly create, edit and delete cookies without leaving your tab.",
"offline_enabled": true,
Expand Down
2 changes: 1 addition & 1 deletion manifest.safari.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "Cookie-Editor",
"version": "1.11.0",
"version": "1.12.0",
"author": "Moustachauve",
"description": "Simple yet powerful Cookie Editor that allow you to quickly create, edit and delete cookies without leaving your tab.",
"offline_enabled": true,
Expand Down
Loading

0 comments on commit fc1cbb7

Please sign in to comment.