码迷,mamicode.com
首页 > Web开发 > 详细

js:浏览器插件

时间:2018-06-06 15:43:10      阅读:504      评论:0      收藏:0      [点我收藏+]

标签:pts   浏览器   ack   sele   man   console   for   code   scripts   

1、chrome

background.js

//chrome.webRequest.onBeforeRequest.addListener(function(info) {
//    chrome.tabs.executeScript({code: ‘console.log("‘ + info.url + ‘")‘});
//    chrome.tabs.getSelected(null, function(tab) {
//        chrome.tabs.sendMessage(tab.id, info.url);
//    });
//    return;
//},{urls: ["<all_urls>"]}, ["blocking"]);

chrome.webRequest.onCompleted.addListener(function(info) {
    //chrome.tabs.executeScript({code: ‘console.log("‘ + info.url + ‘")‘});
    chrome.tabs.getSelected(null, function(tab) {
        chrome.tabs.sendMessage(tab.id, info.url);
    });
    return;
},{urls: ["<all_urls>"]});


chrome.webRequest.onErrorOccurred.addListener(function(info) {
    //chrome.tabs.executeScript({code: ‘console.log("‘ + info.url + ‘")‘});
    chrome.tabs.getSelected(null, function(tab) {
        chrome.tabs.sendMessage(tab.id, "!" + info.error + "!" + info.url);
    });
    return;
},{urls: ["<all_urls>"]});

 

content.js

console.log("content.js");

chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
    console.log(msg);
    document.body.text += msg + "\n";
});

 

manifest.json

{
  "name": "test",
  "version": "1.0",
  "description": "Test",
  "background": {
    "persistent": true, 
    "scripts": ["background.js"]
  },
  "content_scripts": [
    {
      "js": ["content.js"],
      "matches": ["http://*/*", "https://*/*"], 
      "run_at": "document_start"
    }
  ],
  "permissions": [
    "tabs", 
    "<all_urls>",
    "webRequest", 
    "webRequestBlocking"
  ],
  "manifest_version": 2
}

 

 

2、firefox

background.js

//browser.webRequest.onBeforeRequest.addListener(function(info) {
//    browser.tabs.executeScript({code: ‘console.log("‘ + info.url + ‘")‘});
//    return;
//},{urls: ["<all_urls>"]}, ["blocking"]);

browser.webRequest.onCompleted.addListener(function(info) {
    //chrome.tabs.executeScript({code: ‘console.log("‘ + info.url + ‘")‘});
    browser.tabs.query({currentWindow: true,active: true}).then(function (tabs){
        for (let tab of tabs) {
            browser.tabs.sendMessage(tab.id,info.url);
        }
    });
    return;
},{urls: ["<all_urls>"]});

browser.webRequest.onErrorOccurred.addListener(function(info) {
    //chrome.tabs.executeScript({code: ‘console.log("!‘ + info.url + ‘")‘});
    browser.tabs.query({currentWindow: true,active: true}).then(function (tabs){
        for (let tab of tabs) {
            browser.tabs.sendMessage(tab.id,"!" + info.error + "!" + info.url);
        }
    });
    return;
},{urls: ["<all_urls>"]});

 

content.js

//console.log("content.js");

browser.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
    console.log(msg);
    document.body.text += msg + "\n";
});

 

manifest.json

{
    "manifest_version": 2,
    "name": "adtest",
    "version": "1.0",
    "description": "get urlinfo where access a website",
    "background": {
        "scripts": ["background.js"]
    },
    "content_scripts": [
        {
          "js": ["content.js"],
          "matches": ["http://*/*", "https://*/*"], 
          "run_at": "document_start"
        }
    ],
    "permissions": [
        "tabs", 
        "<all_urls>",
        "webRequest", 
        "webRequestBlocking"
    ],
    "applications": {
       "gecko": {
            "id": "borderify@example.com"
       }
    }
}

 

js:浏览器插件

标签:pts   浏览器   ack   sele   man   console   for   code   scripts   

原文地址:https://www.cnblogs.com/channy14/p/9144310.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!