0

I have a chrome extension that needs to work/execute only on a specific sub subfolder of a website but not on other parts of the website

I want it to execute only on https://example.com/subfolder1/sub_subfolder11/ but not on https://example.com/subfolder1/ or https://example.com/subfolder2/ or https://example.com/subfolder3/

This is the manifest.

 "permissions": [
    "storage",
    "declarativeContent",
    "https://example.com/subfolder1/subfolder2/*",
    "tabs", 
    "activeTab"

  ],
  "manifest_version": 2,
  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },

  "web_accessible_resources": [
                            "style.css"
                        ],
  "content_scripts": [
    {
      "all_frames": true, 
      "css":        ["style.css"],
      "matches": ["https://example.com/subfolder1/sub_subfolder11/*"],
      "run_at": "document_end"
    }

The extension works/executes everywhere not just on https://example.com/subfolder1/sub_subfolder11/ Any idea what I'm doing wrong?

UPDATED SINCE I CAN'T COMMENT SUCH A LONG TEXT

Hi @wOxxOm , I have seen you around on many other questions so I would like to thank you for being so helpful.

Related to my issue, I have two tabs ( or more tabs ) on chrome. On one I have the https://example.com/subfolder1/sub_subfolder11/* open and that tab content has links that point to https://example.com/subfolder1/ or https://example.com/subfolder2/ or https://example.com/subfolder3/ and those open in a new tab ( <a target="_blank"... ).

I have tried also manually entering the addresses ( https://example.com/subfolder1/ or https://example.com/subfolder2/ or https://example.com/subfolder3/ ) by opening new tabs but still, the extension gets executed.

On background.js I have tried having nothing there and also having chrome.webRequest.onBeforeRequest.addListener ( to block specific javascript links from loading ) but still the same.

The idea is to block scripts from loading on https://example.com/subfolder1/sub_subfolder11/* but don't block those scripts in a new tab when I go manually to https://example.com/subfolder1/ or https://example.com/subfolder2/ or https://example.com/subfolder3/

Thank you

user13268984
  • 35
  • 1
  • 4
  • Without seeing background.js I can only guess: 1) you use insertCSS or executeScript to inject the code in addition to content_scripts or 2) that site is a SPA (aka AJAX-driven site) that uses "soft" navigation via History API or URL hash modification. The first case is simple to fix (simply remove that code) but the second case requires manual management of the injected CSS which cannot be achieved via `content_script` declaration, you'll have to [detect navigation](/a/39508954) and add/remove the style as a DOM element in the content script. – wOxxOm Apr 09 '20 at 12:27
  • @wOxxOm , just updated my question. Thank you – user13268984 Apr 09 '20 at 17:53
  • Sounds like you didn't reload the extension on `chrome://extensions` page after editing manifest.json. – wOxxOm Apr 09 '20 at 17:57
  • @wOxxOm , I tried everything. Doing the reload and also removing the extension and loading it again from the UNPACKED. Still same – user13268984 Apr 09 '20 at 18:44
  • Well, the only other explanation matching your description - assuming the URLs don't have `#` in them - is a bug in Chrome so you can report it on https://crbug.com. That said, my suggestion to detect navigation and toggle a DOM style element should still work. – wOxxOm Apr 09 '20 at 18:48
  • @wOxxOm the website is a SPA but should it matter if Im opening the URLs on new tab manually? – user13268984 Apr 09 '20 at 18:55
  • It depends on whether there is `#` in the URL. – wOxxOm Apr 09 '20 at 18:59
  • No, there isnt a # on the pages that I dont want the extension to run. Maybe another way to explain what im trying to do is: to block http://example2.com/js.js from loading on https://example.com/subfolder1/sub_subfolder11/ but allow it for https://example.com/subfolder2 the http://example2.com/js.js is intentional, not a typo. Its practically a tracking script that I dont want to be included/executed on https://example.com/subfolder1/sub_subfolder11/ Thank you – user13268984 Apr 09 '20 at 19:18
  • Let's go to the basics. What specifically do you mean by "extension executes"? From your description I could only infer you're talking about CSS you're applying via content_scripts section. – wOxxOm Apr 09 '20 at 19:27
  • I guess you're confusing content scripts with background scripts and webRequest API. All of these are different things for different parts with different API and different targets. So I guess the ultimate problem is about blocking a resource when the tab has a certain URL. – wOxxOm Apr 09 '20 at 19:52

1 Answers1

1

Looks like you expected content_scripts delcaration to apply to the background script and webRequest API. All of these are different things for different parts of an extension.

Assuming the ultimate goal is to block a resource when the tab has a certain URL path you'll have to maintain a map of tab ids and URLs, which will be used the webRequest listener:

const tabsToHook = {};

chrome.tabs.onUpdated.addListener((tabId, info) => {
  if (info.url) {
    if (info.url.startsWith('https://example.com/path1')) {
      tabsToHook[tabId] = true;
    } else {
      delete tabsToHook[tabId];
    }
  }
});

chrome.tabs.onRemoved.addListener(tabId => delete tabsToHook[tabId]);
chrome.tabs.onReplaced.addListener((_, removed) => delete tabsToHook[removed]);

chrome.webRequest.onBeforeRequest.addListener(info => ({
  cancel: tabsToHook[info.tabId],
}), {
  urls: ['http://example.com/some.js'],
  types: ['script'],
}, ['blocking']);

FWIW in Firefox 52+ webRequest listeners can return a Promise so the entire code would be:

browser.webRequest.onBeforeRequest.addListener(async info => {
  const {url} = await browser.tabs.get(info.tabId);
  return {cancel: url.startsWith('https://example.com/path1')};
}, {
  urls: ['http://example.com/some.js'],
  types: ['script'],
}, ['blocking']);
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • Thank you for that. I tried adding that code to background.js persistent false and it show `The 'webRequest' API cannot be used with event pages.` I tried with persistent true and it only executes once. No sure where that code needs to be. – user13268984 Apr 09 '20 at 22:06
  • I won't be able to help you further because I still don't know what you mean by "execute". It took me long enough to arrive at a guess in this answer and I'm somewhat tired of guessing as this is not very productive. – wOxxOm Apr 10 '20 at 04:07
  • you did more than enough already :) . Sorry for not being able to explain better the issue. I used the code you provided with some modifications and now it works as it should. Just accepted your answer . Have a great day – user13268984 Apr 10 '20 at 04:26