Stephane Rolland
Stephane Rolland

Reputation: 39896

Modifying the content of a webpage using content_scripts

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 ?

Upvotes: 0

Views: 132

Answers (1)

Matthew
Matthew

Reputation: 49

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.

Upvotes: 1

Related Questions