Skip to content

Commit 6616843

Browse files
author
priyank
committed
Improved support for 'apply styles automatically' in Kiwi browser
1 parent e916520 commit 6616843

File tree

5 files changed

+88
-39
lines changed

5 files changed

+88
-39
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* global chrome */
2+
3+
const runningInKiwiExtensionLikeEnvironment = function () {
4+
// An error shouldn't occur. This try...catch block is just for extra safety.
5+
try {
6+
const manifest = chrome.runtime.getManifest();
7+
if (manifest.name.indexOf('Kiwi Browser') !== -1) {
8+
return true;
9+
}
10+
} catch (e) {
11+
// do nothing
12+
}
13+
14+
return false;
15+
};
16+
17+
export {
18+
runningInKiwiExtensionLikeEnvironment
19+
};

extension/manifest-generator.mjs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ var generateManifest = function (whichBrowser) {
2020
"manifest_version": 3,
2121
"author": "Priyank Parashar",
2222
"default_locale": "en",
23-
"name": "__MSG_Extension_Name__",
23+
"name": (function () {
24+
if (whichBrowser === "kiwi") {
25+
// Note: This name is utilized elsewhere to detect the Kiwi Browser
26+
return "__MSG_Extension_Name__ for Kiwi Browser";
27+
} else {
28+
return "__MSG_Extension_Name__";
29+
}
30+
})(),
2431
"description": "__MSG_Extension_Description__",
2532
"homepage_url": "https://github.com/webextensions/live-css-editor",
2633

extension/manifest-kiwi.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"manifest_version": 3,
44
"author": "Priyank Parashar",
55
"default_locale": "en",
6-
"name": "__MSG_Extension_Name__",
6+
"name": "__MSG_Extension_Name__ for Kiwi Browser",
77
"description": "__MSG_Extension_Description__",
88
"homepage_url": "https://github.com/webextensions/live-css-editor",
99
"icons": {

extension/scripts/background-magicss-include.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import { randomUUID } from './utils/randomUUID.js';
1818
import { extLib } from './chrome-extension-lib/ext-lib.js';
1919
import { basisNumberFromUuid } from './utils/basisNumberFromUuid.js';
2020

21+
import { runningInKiwiExtensionLikeEnvironment } from '../commonAppUtils/detectEnvironment.js';
22+
2123
import { mainFnMetricsHandler } from './appUtils/mainFnMetricsHandler.js';
2224
import { myWin } from './appUtils/myWin.js';
2325
import { nativeAlert } from './appUtils/nativeAlert.js';
@@ -598,6 +600,18 @@ var runningInFirefoxExtensionLikeEnvironment = function () {
598600
}
599601
};
600602

603+
var runningExtensionInStaticPermissionsEnvironment = function () {
604+
if (
605+
// TODO: Move to optional_permissions when Firefox supports it and refactor this code
606+
runningInFirefoxExtensionLikeEnvironment() ||
607+
runningInKiwiExtensionLikeEnvironment()
608+
) {
609+
return true;
610+
} else {
611+
return false;
612+
}
613+
};
614+
601615
var informUser = function (config) {
602616
var message = config.message,
603617
tab = config.tab || {},
@@ -1184,7 +1198,7 @@ var onDOMContentLoadedHandler = function () {
11841198
// do nothing
11851199
} else {
11861200
// tab.url would not be available for a new tab (eg: new tab opened by Ctrl + T)
1187-
if (runningInFirefoxExtensionLikeEnvironment()) { // TODO: Move to optional_permissions when Firefox supports it and refactor this code
1201+
if (runningExtensionInStaticPermissionsEnvironment()) {
11881202
(async () => {
11891203
await reapplyCss(tabId);
11901204
})();

extension/scripts/magicss/magicss.js

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
} from '../utils/StyleTag.js';
2020

2121
import { myWin } from '../appUtils/myWin.js';
22+
import { runningInKiwiExtensionLikeEnvironment } from '../../commonAppUtils/detectEnvironment.js';
2223

2324
import { extLib } from '../chrome-extension-lib/ext-lib.js';
2425
import sourceMap from '../3rdparty/source-map.js';
@@ -3777,45 +3778,53 @@ var chromePermissionsContains = function ({ permissions, origins }) {
37773778
}
37783779

37793780
try {
3780-
chrome.runtime.sendMessage(
3781-
{
3782-
requestPermissions: true,
3783-
requestWebNavigation: true,
3784-
tabOriginWithSlash
3785-
},
3786-
async function asyncCallback(status) {
3787-
if (chrome.runtime.lastError) {
3788-
console.log('Error message reported by Magic CSS:', chrome.runtime.lastError);
3789-
alertNote(
3790-
'Error! Unexpected error encountered by Magic CSS extension.<br />You may need to reload webpage & Magic CSS and try again.',
3791-
10000
3792-
);
3793-
}
3794-
if (status === 'request-granted') {
3795-
await markAsPinnedOrNotPinned(editor, 'pinned');
3796-
if (myWin.flagEditorInExternalWindow) {
3797-
chromeRuntimeMessageIfRequired({
3798-
type: 'magicss',
3799-
subType: 'mark-as-pinned-without-notification'
3781+
const onRequestGranted = async function () {
3782+
await markAsPinnedOrNotPinned(editor, 'pinned');
3783+
if (myWin.flagEditorInExternalWindow) {
3784+
chromeRuntimeMessageIfRequired({
3785+
type: 'magicss',
3786+
subType: 'mark-as-pinned-without-notification'
3787+
});
3788+
}
3789+
alertNote(
3790+
'<span style="font-weight:normal;">Now onwards, </span>styles would be applied automatically <span style="font-weight:normal;">even without loading this extension<br/>(for pages on <span style="text-decoration:underline;">' + tabOriginWithSlash + '</span>)</span>',
3791+
10000
3792+
);
3793+
sendEventMessageForMetrics({
3794+
name: 'applyStylesAutomaticallyPinComplete',
3795+
spot: 'header'
3796+
});
3797+
};
3798+
3799+
if (runningInKiwiExtensionLikeEnvironment()) {
3800+
onRequestGranted();
3801+
} else {
3802+
chrome.runtime.sendMessage(
3803+
{
3804+
requestPermissions: true,
3805+
requestWebNavigation: true,
3806+
tabOriginWithSlash
3807+
},
3808+
async function asyncCallback(status) {
3809+
if (chrome.runtime.lastError) {
3810+
console.log('Error message reported by Magic CSS:', chrome.runtime.lastError);
3811+
alertNote(
3812+
'Error! Unexpected error encountered by Magic CSS extension.<br />You may need to reload webpage & Magic CSS and try again.',
3813+
10000
3814+
);
3815+
}
3816+
if (status === 'request-granted') {
3817+
await onRequestGranted();
3818+
} else if (status === 'request-not-granted') {
3819+
alertNote('You need to provide permissions to reapply styles automatically', 10000);
3820+
sendEventMessageForMetrics({
3821+
name: 'applyStylesAutomaticallyPinIncompleteDueToPermission',
3822+
spot: 'header'
38003823
});
38013824
}
3802-
alertNote(
3803-
'<span style="font-weight:normal;">Now onwards, </span>styles would be applied automatically <span style="font-weight:normal;">even without loading this extension<br/>(for pages on <span style="text-decoration:underline;">' + tabOriginWithSlash + '</span>)</span>',
3804-
10000
3805-
);
3806-
sendEventMessageForMetrics({
3807-
name: 'applyStylesAutomaticallyPinComplete',
3808-
spot: 'header'
3809-
});
3810-
} else if (status === 'request-not-granted') {
3811-
alertNote('You need to provide permissions to reapply styles automatically', 10000);
3812-
sendEventMessageForMetrics({
3813-
name: 'applyStylesAutomaticallyPinIncompleteDueToPermission',
3814-
spot: 'header'
3815-
});
38163825
}
3817-
}
3818-
);
3826+
);
3827+
}
38193828
} catch (e) {
38203829
handleUnrecoverableError(e);
38213830
}

0 commit comments

Comments
 (0)