0

I want to change the tab like edge://extensions/ or extension setting page title to add a '*' at the end of the original title if get the selection context on any page after mouseup event.

I create a simple Chrome extension.

change.js

var title = document.title;
    document.addEventListener("selectionchange", function () {

        if (!window.getSelection().toString().trim()) {
            document.title = title;
        }
    });
    document.addEventListener('mouseup', function (e) {
        var text = window.getSelection().toString().trim();
        if(text) {
            document.title += '*';
        } else if (!text) {
            document.title = title;
        }
    });

The manifest.json file. It shows an error when I add it on the extension page.

Error: Invalid value for 'content_scripts[0].matches[0]': Invalid host wildcard.

{
"update_url": "https://example.com",

    "name": "change",
    "short_name": "change",
    "description": "*",

    "version": "1.0",

    "icons": { 
        "16": "images/icon16.png",
        "32": "images/icon32.png",
        "48": "images/icon48.png",
        "128": "images/icon128.png"
    },
    "content_scripts": [
        {
        "matches": ["*://*/*"],
        "js": ["change.js"]
        }
    ],
    
    "manifest_version": 2
}
lyscop
  • 77
  • 1
  • 8
  • `http://*.*/*` should be `*://*/*` or simply `` – wOxxOm Feb 25 '21 at 09:46
  • @wOxxOm Thx, it works on the general page as the script on the Tampermonkey. Another question, does it can not be executed on the edge://extensions/ or the Tempermonkey setting page? – lyscop Feb 25 '21 at 09:55
  • This should work in all chromium-based browsers: [Can you access chrome:// pages from an extension?](https://stackoverflow.com/a/19046796) – wOxxOm Feb 25 '21 at 09:56
  • @wOxxOm I execute the code in the mouseup event to change the title on the console, it works well if there is a selection context on the edge://extensions page, but I use the mouse to get the selection context, there is no response. – lyscop Feb 25 '21 at 10:15
  • I don't understand. – wOxxOm Feb 25 '21 at 10:27
  • @wOxxOm Sorry, I don't describe it clearly. I delete the mouseup event to execute it on edge://extensions, and it works well, but the extension does not work if I use the mouse to select a word. https://i.imgur.com/OBhy3ln.png – lyscop Feb 25 '21 at 10:36
  • You can simplify your code like this: var {title} = document; document.onselectionchange = () => { var sel = getSelection().toString().trim(); document.title = sel ? sel + '*' : title; } – wOxxOm Feb 25 '21 at 10:55
  • @wOxxOm Emmm, the result in the picture is well, it is just the code on the console that I delete `document.addEventListener('mouseup', function (e) {`, it is said that I can't use the mouse to select a word then auto change the original title to adds a '*' in the end. But it works well on Google or other sites except for edge://extensions – lyscop Feb 25 '21 at 10:58
  • I don't understand. The code I've shown works on `edge://extensions` – wOxxOm Feb 25 '21 at 11:07
  • @wOxxOm Thank you for your patience, the code works well on the console, but the extension does work. https://lyscop.lanzous.com/iM9tXm69swh – lyscop Feb 25 '21 at 11:24
  • I don't understand Chinese. I guess you didn't specify the correct URL in `matches`. – wOxxOm Feb 25 '21 at 11:33
  • @wOxxOm Sorry, it is the extension file, I upload it to the wrong driver. https://drive.google.com/file/d/1pBby03u-8gciCJoqyJnmaMveWabDO9E1/view – lyscop Feb 25 '21 at 11:46
  • It works if you add `chrome://extensions/` in `matches` as the answer I linked explains. Also, you've copied my code incorrectly in change.js, it should be `sel ? sel` but yours is `sel ? title` – wOxxOm Feb 25 '21 at 11:54
  • @wOxxOm Is this right? and I set Enable #extensions-on-edge-urls `"matches": [ "", "chrome://extensions/", "extension://*" ],` – lyscop Feb 26 '21 at 15:29

0 Answers0