Chrome扩展程序实时解锁网站

时间:2019-12-09 22:21:11

标签: javascript html css google-chrome google-chrome-extension

首先,我知道这个任务已经被问了一百万遍了,但是似乎没有任何效果。基本上是chrome扩展程序,它将在某些时间阻止某些网站。一切正常,除了功能,一旦时间到,以前被阻止的网站就可以访问。如果我更新时间,则该网站实际上可以被重新阻止,但是无论我做什么,它都不会被阻止。无论如何 清单

{
"manifest_version": 2,
"name": "Block Station",
"description": "This extension is used to block access to certain sites during busy times",
"version": "1.0",

"browser_action": {
  "default_icon": "image/eye-blocked.png",
  "default_popup": "main.html"
},
"background": {
    "scripts": ["js/engine/init.js", "js/background.js"]
  },
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["js/jquery-3.4.1.min.js"]
    }
  ],
  "permissions": [
    "webRequest",
    "webRequestBlocking",
    "webNavigation",
    "tabs",
    "activeTab",
    "http://*/*",
    "storage",
    "https://*/*"
]

}

background.js 这是从存储中获取数据并将其传递给init.js来处理阻止功能的地方

console = chrome.extension.getBackgroundPage().console;
//this listens for change in the browser for all the websites


chrome.webRequest.onBeforeRequest.addListener((details)=>{
  getData()

},
  {urls: ["<all_urls>"]}
);

//runn function everytime a new tab is open

//Check the database to see if userData current exists

  chrome.runtime.onMessage.addListener(
      function(message, sender, sendResponse) {
        if (message.method === 'onSumbit' || message.editedState ) {          
          sendResponse(getData())
        }
      }
  )

// communicates wth the background and get response from it

async function  getPython(data) {  
    return extensionLogicHandler(data);

}

function getData (){

   chrome.storage.sync.get("userData", function(items) {
    let success = {
      code:0,
      message:""
    }
    if (items.userData) {
      return getPython(items.userData)


      }
      return success;
  })

}

基本上,从background.js传递的数据用于检查当前时间是否与用户输入的时间匹配,并阻止该时间段内用户列出的网站

function blockHandler(websites) {
    function blockRequest(details) {
        return {cancel: true};
     }
     console.log(toBlock, websites);
     chrome.webRequest.onBeforeRequest.addListener(blockRequest,
        {urls: websites},
        ["blocking"]);

     if(websites.length === 0){
        chrome.webRequest.onBeforeRequest.removeListener(blockRequest)

     }

}

const blockWebsiteHandler = (websites) =>{
    let modifiedWebsites= []   
    if(websites.length > 0){ 
        websites.forEach(website => 
            modifiedWebsites.push(`*://*.${website}/*`, `*://www.${website}/*`)
            )

        blockHandler(modifiedWebsites)
    }
    else{
        blockHandler(websites)

    }


}




const extensionLogicHandler = (data) =>{
    let today = new Date(); 
    //Checks if the user works weekends or not
    let {username, 
            workStart, 
            workEnd, 
            blacklist} = data

        if (workStart <= today && 
            today < workEnd && 
            !isWeekend(today)
            ){            
            blockWebsiteHandler(blacklist)
        }
        else{
            blockWebsiteHandler(Array(0))
        }
    }

0 个答案:

没有答案