Recently want to change huawei mobile phone, rob for a long time did not get, want to write a code to automatically rob, after all, can write code to solve the matter is the programmer should do. I just wrote a Google plugin. First, write a configuration file manifest.json

{// The manifest file version, which must be written, must be 2 "Manifest_version ": 2, // the plug-in name" name": "demo", // the plug-in version" version": "ICONS ": {"16": "img/icon.png", "48": "Img/icon. PNG", "128", "img/icon. PNG}", / / would have been in the background of JS or background page "background" : {/ / 2 kind of specified way, if you specify a JS, then automatically generates a background page "page" : "background.html" //"scripts": ["js/ background-.js "]}, "browser_action": {"default_icon": "Img /icon.png", // icon hover when the title, optional "default_title":" This is an example Chrome plugin ", "default_popup": /*"page_action": {"default_icon": "img/icon.png", "default_title": "I'm pageAction", "default_popup": "popup.html"},*/ // need to directly inject the page JS "content_scripts": [{//"matches": [" http:// * / * ", "https:// * / *"], / / "< all_urls >" means to match all address "matches" : [" < all_urls > "], / / multiple JS injection in sequence "JS" : ["js/jquery-1.8.3.js", "js/content-script.js"], // js injection can be a little bit more informal, but the CSS should be careful, because not careful, it can affect the global style "CSS ": [" CSS /custom.css"], // code injection time, optional values: "Document_start ", "document_end", or "document_idle", the last one indicates when the page is idle, the default document_idle" run_at": "Document_start"}, // This is just to demonstrate that content-script can be configured with multiple rules {"matches": ["*://*/*.png", "*://*/*.jpg", "*://*/*.gif", "*://*/*.bmp"], "js": ["js/show-image-content-size.js"]}], "permissions": ["contextMenus", // right-click "tabs", // tabs" notifications", // Web request "webRequestBlocking", "storage", // Plugins are stored locally "http://*/*", // can be accessed via executeScript or insertCSS website "https://*/*" // can be accessed via executeScript or insertCSS website], // A list of plugin resources that can be accessed directly by a common page. If not set, "web_accessible_resources" cannot be accessed directly: ["js/inject.js"], // pluginhome page, this is very important, don't waste this free AD space "homepage_URL ": "https://www.baidu.com", // override the default browser TAB "chrome_url_overrides": {// override the default browser TAB "newtab": "Newtab.html"}, // plugin configuration page before Chrome40 write "options_page": "Options_ui ": {"page": "options_ui": {"page": "Options. HTML ", // add some default styles, it is recommended to use "chrome_style": true}, // register a keyword to the address bar to provide search suggestions, only one keyword can be set "omnibox": {"keyword" : "Devtools_page ": "devtools.html"}, // Default language "default_locale": "zh_CN", // devTools page entry, note that you can only point to an HTML file, not a JS file "devtools_page": "devtools.html"}Copy the code

Create a new folder, create a few empty files, configure manifest.json, go to the browser extension, open developer mode, and click Load the decompressed extension.

My configuration file is set to the following,

{" Manifest_version ": 2, // The manifest_version file, this must be written and must be 2 "name": "HUAWEI", // the plug-in name" version": "1.0.0", // the plug-in version" description": "Content_scripts ": // You need to inject javascript directly into the page, you can add multiple rules [{"matches": ["<all_urls>"], // "<all_urls>" means match all addresses this must be written otherwise it will give an error "js": ["js/jquery-1.8.3.js", "js/content-script.js"], "web_accessible_resources": ["js/jquery-1.8.3.js", "js/content-script.js"] ["js/inject.js"]// / a list of plug-in resources that can be accessed directly by ordinary pages}Copy the code

The background is a resident page that has the longest lifetime of any type of page in the plugin. It opens when the browser is opened and closes when the browser is closed, so it is common to put in the background any code that needs to run all the time, on launch, and globally.

Background has very high permissions to call almost all of Chrome’s extension apis (except DevTools), and it has unlimited cross-domain access to any site without requiring CORS. But background can not get the DOM operation, can not achieve my requirements, configure content_scripts to inject JS file into the page, can operate the DOM, can realize the function of simulating click, the introduction of JQ is to save code, do not have to write a set of code to get DOM, save labor. The code for the content-scripts.js file is as follows. Inject the JS file into the page, and you can manipulate the DOM in the injected file

injectCustomJs() function injectCustomJs(jsPath) { jsPath = jsPath || 'js/inject.js'; var temp = document.createElement('script'); temp.setAttribute('type', 'text/javascript'); / / similar to obtain the address of the chrome - the extension: / / ihcokhadfjfchaeagdoclpnjdiokfakg/js/inject js temp. SRC = chrome. The extension. The getURL (jsPath); Temp. Onload = function () {/ / in the page not good-looking, execution of the ward away with this. ParentNode. RemoveChild (this); }; document.head.appendChild(temp); }Copy the code

First choose the color and version of the model

$(" span: the contains (' white glaze ') ") && $(" span: the contains (' white glaze ') "). Click () $(" span: the contains (' 5 g netcom 8 gb + 256 gb ') ") && $(" contains(' 5gb +256GB')").click()Copy the code

Use jq to get these 2 elements and then use the short-circuit operator to operate on them. This way you can select the color and the version when the page is finished loading and then determine whether the element is in the waiting state by getting the countdown and then refresh the page with 2 seconds left

let len = $('#pro-operation-countdown ul span').length let timelI = $('#pro-operation-countdown ul span') let day = timelI.eq(0).html() let hour = timelI.eq(1).html() let min = timelI.eq(2).html() let second = timelI.eq(3).html() if(dis = = 'block') {if (day = = '00' && hour = = '00' && min = = '00' && second = = '02') {log (' countdown 2 seconds! ') location.reload() } }Copy the code

Then you get the element of the button, and you can see that the button on the page has a disabled property that you can use to determine whether it’s clickable

let len = $('#pro-operation-countdown ul span').length let timelI = $('#pro-operation-countdown ul span') let day = timelI.eq(0).html() let hour = timelI.eq(1).html() let min = timelI.eq(2).html() let second = timelI.eq(3).html() let dis = $('#pro-operation-countdown').css('display') let pd2 = $('#product-button02') pd2.click() if(dis == 'block'){ If (day = = '00' && hour = = '00' && min = = '00' && second = = '02') {log (' countdown 2 seconds! ') location.reload() } }else if($('#pro-operation') && pd2.classList.indexOf('disabled') < 0){ pd2.click() }Copy the code

And then according to the previous situation of the snap up to simulate the click of the button to enter the queue after the failure of the popover back to the activity, and then in order to accommodate the various cases to write in all the possible

$(" span: the contains (' return activities') ") && $(" span: the contains (' return activities') "). Click () $(" div: the contains (' return activities') ") && $(" div: the contains (' return activities') "). Click () $(" ul: the contains (' return activities') ") && $(" ul: the contains (' return activities') "). Click () $(" li: the contains (' return activities') ") && $(" li: the contains (' return activities') "). Click () $(" p: the contains (' return activities') ") && $(" p: the contains (' return activities') "). Click () $(" a: the contains (' return activities') ") && $(" contains(' contains ')").click()Copy the code

If you get it, you need to submit an order, so there is a submit order button, and you need to be compatible with it as above

$(" span: the contains (' submit order ') ") && $(" span: the contains (' submit order ') "). Click () $(" div: the contains (' submit order ') ") && $(" div: the contains (' submit order ') "). Click () $(" ul: the contains (' submit order ') ") && $(" ul: the contains (' submit order ') "). Click () $(" li: the contains (' submit order ') ") && $(" li: the contains (' submit order ') "). Click () $(" p: the contains (' submit order ') ") && $(" p: the contains (' submit order ') "). Click () $(" a: the contains (' submit order ') ") && $(" contains(' contains ')").click()Copy the code

One of the problems I found during the grab was that the page had a problem with the countdown. At 9:58, there were 37 minutes left in the countdown. Later I added the timer to refresh it and added a domain name judgment so that every page didn’t have to run