chrome扩展开发实战入门之二-自动搜索

目标:产生随机数,用于百度搜索;像看电视一样观看搜索结果

参考上一篇,新建目录hellocrx,其中三个文件:manifest.json  content_script.js 和jquery-3.4.1.min.js(网上很多)

manifest.json

{
  "manifest_version": 2,
  "name": "hellocrx",
  "version": "1.0.0",  
  "description": "crx入门学习",
  "content_scripts": [
    {
    "matches": ["*://*.baidu.com/*"],
    "js": ["jquery-3.4.1.min.js","content_script.js"],
    "run_at": "document_end",
    "all_frames": true
    }
  ],
  "browser_action": {
    "default_title": "mycrx test"    
    },
  "permissions": [
    "bookmarks", "http://*/*", "https://*/*","contextMenus","tabs","activeTab"
    ]
}

content_script.js

console.log("baidu搜索");
$(document).ready(function(){ 
    function search(){
        var kw = Math.ceil(Math.random()*10000).toString();                 
        $("#kw").val(kw);
        $("#su").click();
        setTimeout(e=>{location.reload();},500);          
    } 
    setTimeout(search,5000);    
});

说明:由于有"matches": ["*://*.baidu.com/*"],在加载百度页面时,会立即执行content_script,并在5秒后执行search(),

而search()中的location.reload()又使得content_script重复执行。

本想总结一下chrome扩展的一些概念,懒得费事了,因为这篇https://www.cnblogs.com/liuxianan/p/chrome-plugin-develop.html已经总结的很好了,包括不同脚本之间的通信 、权限等。 以及这个https://www.cnblogs.com/he-bo/p/9963540.html

参考:https://blog.csdn.net/lchina1314/article/details/84725084

原文地址:https://www.cnblogs.com/pu369/p/12041688.html