Skip to content

Updated manifest to v3 and added "css" to content scripts#39

Open
vittoopugliese wants to merge 1 commit into
iamdiogo:masterfrom
vittoopugliese:master
Open

Updated manifest to v3 and added "css" to content scripts#39
vittoopugliese wants to merge 1 commit into
iamdiogo:masterfrom
vittoopugliese:master

Conversation

@vittoopugliese

Copy link
Copy Markdown
Contributor

I have noticed that the extension stopped working at least on my Chrome and it seems that updating the manifest to v3 and loading the "css_dark.css" in the "css" prop inside "content_scripts" fixes it, also changed the version in case this is approved, but I don't know if that's correct.
I am trying to resolve some CSS issues that are open, I will push them sooner!
I hope this helps! Please tell me if there's something wrong..

Comment thread manifest.json
},
"background": {
"scripts": ["background.js"]
"service_worker": "background.js"

@PaulW-NHS PaulW-NHS Aug 1, 2024

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is required given what this plugin is doing, albeit I've only tested this on Chrome, not Edge. The unpacked extension works as it previously did with the changes I mention below, but requires more testing.

@PaulW-NHS

Copy link
Copy Markdown

Testing the above it didn't work for me so I've made some adjustments.

{ "name": "DarkCloud", "description": "SoundCloud Dark Theme", "version": "2024.07.30", "permissions": ["scripting"], "manifest_version": 3, "icons": { "128": "darklogo.png" }, "action": { "default_title": "DarkCloud", "default_icon": "darklogo.png" }, "web_accessible_resources": [ { "resources": ["css_dark.css"], "matches": ["https://soundcloud.com/*"] } ], "content_scripts": [ { "matches": ["https://soundcloud.com/*"], "js": ["background.js"], "css": ["css_dark.css"], "run_at": "document_start" } ] }

and then updating the background.js

const observer = new MutationObserver((mutationsList) => {
    mutationsList.forEach((mutation) => {
        mutation.removedNodes.forEach((node) => {
            if (node.id === 'darkstyle_css') {
                const head = document.getElementsByTagName('head')[0];
                const link = document.createElement('link');

                link.id = 'darkstyle_css';
                link.rel = 'stylesheet';
                link.type = 'text/css';
                link.href = chrome.extension.getURL('css_dark.css');
                link.media = 'all';

                head.appendChild(link);
            }
        });
    });
});

observer.observe(document, { childList: true, subtree: true });

@josep11

josep11 commented Oct 23, 2024

Copy link
Copy Markdown

let's merge that one @iamdiogo @MyNameIsJulian!

@MAZ01001

Copy link
Copy Markdown

@CTRPaul

and then updating the background.js

I would change this a bit so it stops checking the rest of the mutated nodes when it has already re-added the CSS and keeps a reference to the link element instead of creating a new one every re-adding

// background.js

const link = document.createElement('link');

link.id = 'darkstyle_css';
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = chrome.extension.getURL('css_dark.css');
link.media = 'all';

document.head.appendChild(link);

const observer = new MutationObserver((mutationsList) => {
    'use strict';
    for (const mutation of mutationsList) {
        for (const node of mutation.removedNodes) {
            if (node.id !== 'darkstyle_css') continue;
            document.head.appendChild(link);
            return;
        }
    }
});

observer.observe(document, { childList: true, subtree: true });
Also, here is the JSON you posted, formatted for ease of reading
{
    "name": "DarkCloud",
    "description": "SoundCloud Dark Theme",
    "version": "2024.07.30",
    "permissions": ["scripting"],
    "manifest_version": 3,
    "icons": {
        "128": "darklogo.png"
    },
    "action": {
        "default_title": "DarkCloud",
        "default_icon": "darklogo.png"
    },
    "web_accessible_resources": [
        {
            "resources": ["css_dark.css"],
            "matches": ["https://soundcloud.com/*"]
        }
    ],
    "content_scripts": [
        {
            "matches": ["https://soundcloud.com/*"],
            "js": ["background.js"],
            "css": ["css_dark.css"],
            "run_at": "document_start"
        }
    ]
}

@MAZ01001

Copy link
Copy Markdown

I just realized for background.js, instead of searching for the node in all of the modified nodes, you can check it directly (if it has a parent node, it's in the DOM)

const link = document.createElement('link');

link.rel = 'stylesheet';
link.type = 'text/css';
link.href = chrome.extension.getURL('css_dark.css');

document.head.appendChild(link);

const observer = new MutationObserver(() => link.parentNode == null && document.head.appendChild(link));

observer.observe(document, { childList: true, subtree: true });

The media attribute in link elements is all by default and since we have a reference stored, an id is also not needed

@MAZ01001 MAZ01001 mentioned this pull request Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants