0

It's not a dupplicate, I have already checked the question Chrome extension: Modifying the content of a webpage but it does not answer my question, since I want to block only at certain hours, thus the blocking/not blocking must be done inside the js code (according to me).

So I have a little chrome extension which I want to block some sites at some hours. I have not put the code checking the time, since it is not relevant to my problem.

manifest.json:

{
"manifest_version": 2,

"name": "Site Blocker Extension",
"description": "This extension blocks some sites at defined hours",
"version": "1.0",

"content_scripts": [
    {
        "matches": ["https://www.facebook.com/*"],
        "js": ["contentScript.js"]
    }
],
"page_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html",
    "default_title": "SiteBlocker Extension"
},

"permissions": ["activeTab","tabs","https://www.facebook.com/*"]

}

contentScript.js:

chrome.tabs.executeScript({
//code: 'document.body.style.backgroundColor="red"'
   code: 'document.body.textContent="<p>Go back to work :-)</p>"'

});

Neither changing the background color to red, nor suppressing the body of the html by changing it seems to work.

It's my first chrome extension: Do you have an idea what I am doing wrong ?

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
  • 1
    1) chrome.tabs is not available inside content scripts 2) it's not needed either because the content script is already running 3) you don't need a declared content script, 4) you need an event page, chrome.alams API, chrome.declarativeContent API 5) make sure to read the [architecture overview](https://developer.chrome.com/extensions/overview#arch). – wOxxOm Mar 16 '18 at 19:35
  • Thanks, I am gonna have a look at event page, chrome.alarms API, and chrome.declarativeContent API. But I am a bit puzzled by your statement: "you don't need a declared content script", because in the link you gave me to read, it says "If your extension needs to interact with web pages, then it needs a content script. ". Can you elaborate on the fact that eventually a content script is not needed to interact with the web page ? What is the main reason that makes the content script optional in your point of view ? – Stephane Rolland Mar 16 '18 at 20:35
  • 1
    There's no need to declare it in manifest.json because you'll inject it programmatically via chrome.declarativeContent or, alternatively, chrome.tabs.executeScript. – wOxxOm Mar 16 '18 at 20:42

1 Answers1

1

Sounds like your a beginner.

I am no expect but been messing with extensions enough to know a bit. You probably wanna ADD a background page to your manafest. Background pages are not like content scripts. A Content script runs every page load, background page runs the entire time chrome is open weather your webpage is open or not.

"background": {
"scripts": ["background.js"]
},

"permissions": [
"webRequest",
"://.facebook.com/*"
]

Also look into WebRequest
https://developer.chrome.com/extensions/webRequest
Blocking request in Chrome

Webrequest you can see every small request going to & free your web browser to the website your on, this is more useful for blocking many or very specific request. Example if you wanna block some Ads, or maybe just 1 picture.

Enjoy the reading, hopefully you'll find some good stuff.

Matthew
  • 49
  • 5
  • I had dabbled with "background" and its associated script, and permissions. Depending on the tab.url I was trying to modify the body (similarily with chrome.tabs.ExecuteScript) inside background.js, but it had no effect. I could launch an alert window though, so I guess the code was interpreted. – Stephane Rolland Mar 16 '18 at 20:24