• js:浏览器插件


    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 + "
    ";
    });

    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
    }

    【另外chrom浏览器插件的编写学习见:https://www.cnblogs.com/champagne/p/4848520.html】

    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 + "
    ";
    });

    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"
           }
        }
    }
    每天努力一点,每天学习一点。 Keep Moving...
  • 相关阅读:
    HDU1255 覆盖的面积 —— 求矩形交面积 线段树 + 扫描线 + 离散化
    HDU1542 Atlantis —— 求矩形面积并 线段树 + 扫描线 + 离散化
    HDU1540 Tunnel Warfare —— 线段树 区间合并
    HDU3974 Assign the task —— dfs时间戳 + 线段树
    HDU4027 Can you answer these queries? —— 线段树 区间修改
    POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值
    ZOJ1610 Count the Colors —— 线段树 区间染色
    HDU1698 Just a Hook —— 线段树 区间染色
    POJ2528 Mayor's posters —— 线段树染色 + 离散化
    POJ3468 A Simple Problem with Integers —— 线段树 区间修改
  • 原文地址:https://www.cnblogs.com/channy14/p/9144310.html
Copyright © 2020-2023  润新知