Skip to content

Commit

Permalink
Fixed Safari
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanvd committed Nov 28, 2024
1 parent ea616e8 commit f654526
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 29 deletions.
26 changes: 26 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,38 @@ const buildExtension = (browser) => {
const commonManifest = "./src/manifests/common.json";
const browserManifest = `./src/manifests/${browser}.json`;

// Helper function to merge content_scripts
const mergeContentScripts = (commonScripts, browserScripts) => {
const mergedScripts = [...commonScripts];

browserScripts.forEach((browserScript) => {
const matchExists = commonScripts.some((commonScript) =>
JSON.stringify(commonScript.matches) === JSON.stringify(browserScript.matches) &&
JSON.stringify(commonScript.js || []) === JSON.stringify(browserScript.js || []) &&
JSON.stringify(commonScript.css || []) === JSON.stringify(browserScript.css || [])
);
if(!matchExists){
mergedScripts.push(browserScript);
}
});

return mergedScripts;
};

// Get the version number from package.json and add it to manifest.json
src([commonManifest, browserManifest])
.pipe(mergeJson({
fileName: "manifest.json",
edit: (json) => {
json.version = packageDef.version;

// Merge content_scripts if present
if(json.content_scripts && Array.isArray(json.content_scripts)){
const commonContentScripts = require(commonManifest).content_scripts || [];
const browserContentScripts = require(browserManifest).content_scripts || [];
json.content_scripts = mergeContentScripts(commonContentScripts, browserContentScripts);
}

return json;
}
}))
Expand Down
7 changes: 7 additions & 0 deletions src/manifests/safari.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@
"storage": {
"managed_schema": "schema.json"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["scripts/screen-shader.js", "scripts/night-mode.js"],
"run_at": "document_start"
}
],
"permissions": ["contextMenus", "tabs", "storage", "webNavigation", "scripting"],
"host_permissions": ["<all_urls>"],
"browser_specific_settings": {
Expand Down
53 changes: 26 additions & 27 deletions src/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,39 +196,32 @@ if(exbrowser != "safari"){
});
}else{
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting
// https://developer.apple.com/documentation/safariservices/assessing-your-safari-web-extension-s-browser-compatibility
// Safari no support "executeScript.injectImmediately"
// Use this content script in iOS 16.4 and higher
// Register the content script only if not already registered
chrome.scripting.getRegisteredContentScripts()
.then((scripts) => {
const isScriptRegistered = scripts.some((script) => script.id === "session-script");
if(!isScriptRegistered){
return chrome.scripting.registerContentScripts([
{
id: "session-script",
js: ["scripts/screen-shader.js", "scripts/night-mode.js"],
matches: ["<all_urls>"],
runAt: "document_start"
}
]);
}
})
.then(() => console.log("registration complete"))
.catch((err) => console.warn("unexpected error", err));
// See manifest.json "content_scripts"
}

// screen-shader.js = Screen Shader
// night-mode.js = Night Mode
const scriptList = ["scripts/screen-shader.js", "scripts/night-mode.js"];
const injectScriptsTo = (tabId, url) => {
if(url.match(/^http/i) || url.match(/^file/i)){
scriptList.forEach((script) => {
chrome.scripting.executeScript({
target: {tabId: tabId},
files: [`${script}`],
injectImmediately: true
}, () => void chrome.runtime.lastError);
});
if(exbrowser != "safari"){
scriptList.forEach((script) => {
chrome.scripting.executeScript({
target: {tabId: tabId},
files: [`${script}`],
injectImmediately: true
}, () => void chrome.runtime.lastError);
});
}else{
scriptList.forEach((script) => {
chrome.scripting.executeScript({
target: {tabId: tabId},
files: [`${script}`]
}, () => void chrome.runtime.lastError);
});
}
}
};
//---
Expand Down Expand Up @@ -987,8 +980,6 @@ function omnidaynightmode(a){
});
}

chrome.runtime.setUninstallURL(linkuninstall);

function initwelcome(){
chrome.storage.sync.get(["firstRun"], function(chromeset){
if((chromeset["firstRun"] != "false") && (chromeset["firstRun"] != false)){
Expand Down Expand Up @@ -1091,5 +1082,13 @@ function installation(){
}

chrome.runtime.onInstalled.addListener(function(){
console.log("hello installation");
installation();
if(chrome.runtime.setUninstallURL){
chrome.runtime.setUninstallURL(linkuninstall);
}
});

chrome.runtime.onStartup.addListener(function(){
console.log("hello startup");
});
4 changes: 2 additions & 2 deletions xcode/Turn-Off-the-Lights-Safari-extension/Config.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
// https://help.apple.com/xcode/#/dev745c5c974

// Update the version number here for all platforms
VERSION = 4.5.3
BUILD_NUMBER = 133
VERSION = 4.5.4
BUILD_NUMBER = 134
Binary file not shown.

0 comments on commit f654526

Please sign in to comment.