InstanceExtensions (Browser)
static class · namespace z3n7 · source Browser/Canvas.cs, Browser/Cookies.cs, Browser/InstanceExtencions.cs
public static class InstanceExtensions
Other parts of this type: InstanceExtensions (Traffic)
Extension methods on Instance: image search on page screenshots, clicks, taps and swipes by coordinates, viewport helpers.
Methods
CenterArea
public static int[] CenterArea(this Instance instance, int width = 0, int height = 0)
Extension method for Instance. source
Area [x, y, width, height] of the given size centred in the viewport.
| Parameter | Description |
|---|---|
width |
Width; 0 together with height = 0 returns the whole viewport. |
height |
Height; 0 means equal to width. |
ClearShit
public static void ClearShit(this Instance instance, string domain)
Extension method for Instance. source
Closes all tabs, clears cache and cookies of domain and opens about:blank.
ClickCenter
public static int[] ClickCenter(this Instance instance)
Extension method for Instance. source
Clicks the viewport centre; returns the point.
ClickImg
public static int[] ClickImg(this Instance instance, string imgFile, int[] searchArea, float threshold = 0.99f, bool nativeSearch = true, int delay = 0)
Extension method for Instance. source
Finds the image and clicks its centre.
| Parameter | Description |
|---|---|
imgFile |
Template image: a file path (.png, .jpg, .jpeg, .gif, .bmp, .webp) or Base64. |
searchArea |
Search area [x, y, width, height] in page pixels. |
threshold |
Required similarity, 0–1. |
nativeSearch |
Use FindImg (default) instead of FindImgFast. |
delay |
Seconds to wait before clicking. |
Returns: The clicked point.
CloseExtraTabs
public static void CloseExtraTabs(this Instance instance, bool blank = false, int tabToKeep = 1)
Extension method for Instance. source
Closes every tab after the first tabToKeep.
| Parameter | Description |
|---|---|
blank |
Then open about:blank in the active tab. |
tabToKeep |
How many tabs to keep. |
CloseNewTab
public static void CloseNewTab(this Instance instance, int deadline = 10, int tabIndex = 2, bool thrw = true)
Extension method for Instance. source
Waits until the number of tabs equals tabIndex and closes all but the first.
| Parameter | Description |
|---|---|
deadline |
Seconds to wait. |
tabIndex |
Tab count to wait for. |
thrw |
Throw when it does not happen in time. |
ConvertToSupportedFormat
public static Bitmap ConvertToSupportedFormat(Bitmap source)
Returns the bitmap in 24bpp RGB (on a white background), the format AForge template matching needs; a 24bpp bitmap is returned as is.
CtrlV
public static void CtrlV(this Instance instance, string ToPaste)
Extension method for Instance. source
Pastes text through the Windows clipboard (Ctrl+V); the previous clipboard text is restored. Errors are ignored.
Down
public static void Down(this Instance instance, int pauseAfterMs = 5000)
Extension method for Instance. source
Closes the browser (launches "without browser") and waits.
| Parameter | Description |
|---|---|
pauseAfterMs |
Pause afterwards, ms. |
F5
public static void F5(this Instance instance, bool WaitTillLoad = true)
Extension method for Instance. source
Reloads the page.
| Parameter | Description |
|---|---|
WaitTillLoad |
Wait for loading to finish. |
FindAllInScreenshot
public static Dictionary<string, List<int[]>> FindAllInScreenshot(this Instance instance, Dictionary<string, string> templates, int[] searchArea, float threshold = 0.9f, int minDistance = 30)
Extension method for Instance. source
Takes one page preview (GetPagePreview) and finds every occurrence of each template in the area. Matches closer than minDistance to a better one are dropped.
| Parameter | Description |
|---|---|
templates |
Name → Base64 image. |
searchArea |
Search area [x, y, width, height] in page pixels. |
threshold |
Required similarity, 0–1. |
minDistance |
Minimum distance between reported centres, px. |
Returns: Name → centres [x, y]; templates without matches or with unreadable images are left out.
FindImg
public static int[] FindImg(this Instance instance, string imgFile, int[] searchArea, double threshold = 0.99)
Extension method for Instance. source
Finds an image in the area with ZennoPoster's own image search.
| Parameter | Description |
|---|---|
imgFile |
Template image: a file path (.png, .jpg, .jpeg, .gif, .bmp, .webp) or Base64. |
searchArea |
Search area [x, y, width, height] in page pixels. |
threshold |
Required similarity, 0–1. |
Returns: Centre [x, y] of the match, or null when not found.
FindImgFast
public static int[] FindImgFast(this Instance instance, string imgFile, int[] searchArea, float threshold = 0.99f, bool thrw = true)
Extension method for Instance. source
Takes one page preview (GetPagePreview) and finds the image in the area with AForge template matching.
| Parameter | Description |
|---|---|
imgFile |
Template image: a file path (.png, .jpg, .jpeg, .gif, .bmp, .webp) or Base64. |
searchArea |
Search area [x, y, width, height] in page pixels. |
threshold |
Required similarity, 0–1. |
thrw |
Throw when not found; otherwise return null. |
Returns: Centre [x, y] of the first match.
FindMultipleInCachedScreenshot
public static Dictionary<string, int[]> FindMultipleInCachedScreenshot(string base64Screenshot, Dictionary<string, string> templates, int[] searchArea, float threshold = 0.95f)
Like FindMultipleInScreenshot, on a screenshot taken earlier.
| Parameter | Description |
|---|---|
base64Screenshot |
Screenshot as Base64. |
templates |
Name → Base64 image. |
searchArea |
Search area [x, y, width, height] in page pixels. |
threshold |
Required similarity, 0–1. |
FindMultipleInMultipleAreas
public static Dictionary<string, int[]> FindMultipleInMultipleAreas(this Instance instance, Dictionary<string, (string template, int[] area)> templatesWithAreas, float threshold = 0.95f)
Extension method for Instance. source
Takes one page preview (GetPagePreview) and finds each template in its own area.
| Parameter | Description |
|---|---|
templatesWithAreas |
Name → (Base64 image, area [x, y, width, height]). |
threshold |
Required similarity, 0–1. |
Returns: Name → centre [x, y]; templates without a match are left out.
FindMultipleInScreenshot
public static Dictionary<string, int[]> FindMultipleInScreenshot(this Instance instance, Dictionary<string, string> templates, int[] searchArea, float threshold = 0.95f)
Extension method for Instance. source
Takes one page preview (GetPagePreview) and finds the first match of each template in the area.
| Parameter | Description |
|---|---|
templates |
Name → Base64 image. |
searchArea |
Search area [x, y, width, height] in page pixels. |
threshold |
Required similarity, 0–1. |
Returns: Name → centre [x, y]; templates without a match are left out.
FixTimezone
public static void FixTimezone(this Instance instance, IZennoPosterProjectModel project)
Extension method for Instance. source
Opens browserscan.net, takes the IP timezone from its visitor-IP request in the traffic and sets it as the instance's IANA timezone.
Remarks: Throws when the response does not arrive within about 60 seconds or has no timezone.
GetCenter
public static int[] GetCenter(this Instance instance)
Extension method for Instance. source
Centre [x, y] of the page viewport (window.innerWidth/innerHeight).
GetCookies
public static string GetCookies(this Instance instance, IZennoPosterProjectModel project)
Extension method for Instance. source
Collects fresh cookies from 5–15 random popular sites with CookieCollector (the profile's user agent and languages, requests sent directly without the instance proxy) and loads them into the instance.
Returns: The cookies in Netscape format.
GetHe
public static HtmlElement GetHe(this Instance instance, object obj, string method = "")
Extension method for Instance. source
Finds an element in the active tab.
| Parameter | Description |
|---|---|
obj |
Element: an HtmlElement; (value, "id") or (value, "name"); or (tag, attribute, pattern, mode, index) as in FindElementByAttribute. |
method |
For a 5-part selector: random picks a random match, last the last one; otherwise the index is used. |
Returns: The element. Throws when it is not found or the selector shape is unsupported.
Go
public static void Go(this Instance instance, string url, bool strict = false, bool waitTdle = false, bool newTab = false)
Extension method for Instance. source
Navigates the active tab unless it is already on the URL.
| Parameter | Description |
|---|---|
url |
Target URL. |
strict |
Compare the whole URL; otherwise skip when the current URL contains it. |
waitTdle |
Wait for loading to finish. |
newTab |
Open a new tab first. |
HeCatch
public static string HeCatch(this Instance instance, object obj, string method = "", int deadline = 10, string atr = "innertext", int delay = 1, string pathToScript = null)
Extension method for Instance. source
Watches for an element that must not appear (e.g. an error message) for deadline seconds.
| Parameter | Description |
|---|---|
obj |
Element: an HtmlElement; (value, "id") or (value, "name"); or (tag, attribute, pattern, mode, index) as in FindElementByAttribute. |
method |
For a 5-part selector: random picks a random match, last the last one; otherwise the index is used. |
deadline |
Seconds to keep looking (every 0.5 s). |
atr |
Attribute used as the exception message. |
delay |
Seconds to wait before starting. |
pathToScript |
Not used. |
Returns: null when the element never appeared. When it appears, throws an exception whose message is its atr.
public static string HeCatch(this Instance instance, IZennoPosterProjectModel project, object obj, string method = "", int deadline = 10, string atr = "innertext", int delay = 1, string pathToScript = null)
Extension method for Instance. source
Same as the overload without project; also stores the message in the err variable before throwing.
| Parameter | Description |
|---|---|
project |
Project for the err variable. |
obj |
Element: an HtmlElement; (value, "id") or (value, "name"); or (tag, attribute, pattern, mode, index) as in FindElementByAttribute. |
method |
For a 5-part selector: random picks a random match, last the last one; otherwise the index is used. |
deadline |
Seconds to keep looking (every 0.5 s). |
atr |
Attribute used as the exception message. |
delay |
Seconds to wait before starting. |
pathToScript |
Not used. |
HeClick
public static void HeClick(this Instance instance, object obj, string method = "", int deadline = 10, double delay = 1, string comment = "", bool thrw = true, bool thr0w = true, int emu = 0, string pathToScript = null)
Extension method for Instance. source
Waits for an element and clicks it after a random pause of about 1–1.3 s × delay.
| Parameter | Description |
|---|---|
obj |
Element: an HtmlElement; (value, "id") or (value, "name"); or (tag, attribute, pattern, mode, index) as in FindElementByAttribute. |
method |
For a 5-part selector: random picks a random match, last the last one; otherwise the index is used. clickOut keeps clicking until the element disappears. |
deadline |
Seconds to keep looking (every 0.5 s). |
delay |
Multiplier of the pause before clicking. |
comment |
Text added to the timeout message. |
thrw |
Throw when the element is not found in time; otherwise return quietly. |
thr0w |
Legacy switch: false also turns thrw off. |
emu |
1 — use full mouse emulation for this action, −1 — turn it off, 0 — leave the instance setting. |
pathToScript |
When set, appends the action and the element's XPath to this file. |
HeDragAndDrop
public static Point HeDragAndDrop(this Instance instance, HtmlElement element, int offsetX, int offsetY = 0)
Extension method for Instance. source
Drags from the element's centre by the given offset with a human-like path: easing, slight vertical wobble and, for longer moves, a small overshoot and correction.
| Parameter | Description |
|---|---|
element |
Element to drag. |
offsetX |
Horizontal offset, px. |
offsetY |
Vertical offset, px. |
Returns: The drop point.
HeDrop
public static void HeDrop(this Instance instance, object obj, string method = "", int deadline = 10, bool thrw = true)
Extension method for Instance. source
Waits for an element and removes it from the page.
| Parameter | Description |
|---|---|
obj |
Element: an HtmlElement; (value, "id") or (value, "name"); or (tag, attribute, pattern, mode, index) as in FindElementByAttribute. |
method |
For a 5-part selector: random picks a random match, last the last one; otherwise the index is used. |
deadline |
Seconds to keep looking (every 0.5 s). |
thrw |
Throw when the element is not found in time; otherwise return quietly. |
HeGet
public static string HeGet(this Instance instance, object obj, string method = "", int deadline = 10, string atr = "innertext", int delay = 1, bool thrw = true, bool thr0w = true, bool waitTillVoid = false, string pathToScript = null)
Extension method for Instance. source
Waits for an element and returns one of its attributes.
| Parameter | Description |
|---|---|
obj |
Element: an HtmlElement; (value, "id") or (value, "name"); or (tag, attribute, pattern, mode, index) as in FindElementByAttribute. |
method |
For a 5-part selector: random picks a random match, last the last one; otherwise the index is used. |
deadline |
Seconds to keep looking (every 0.5 s). |
atr |
Attribute to read. |
delay |
Seconds to wait after finding it. |
thrw |
Throw when the element is not found in time; otherwise return quietly. |
thr0w |
Legacy switch: false also turns thrw off. |
waitTillVoid |
Wait until the element is gone instead; returns null at the deadline and throws while it is present. |
pathToScript |
When set, appends the action and the element's XPath to this file. |
Returns: The attribute value, or null when not found and thrw is false.
HeLongClick
public static void HeLongClick(this Instance instance, object obj, int holdMs = 3, string method = "", int deadline = 10, double delay = 1, string comment = "", bool thrw = true, bool thr0w = true, int emu = 0)
Extension method for Instance. source
Waits for an element and holds the left button at a random point inside it.
| Parameter | Description |
|---|---|
obj |
Element: an HtmlElement; (value, "id") or (value, "name"); or (tag, attribute, pattern, mode, index) as in FindElementByAttribute. |
holdMs |
Hold time in milliseconds. |
method |
For a 5-part selector: random picks a random match, last the last one; otherwise the index is used. |
deadline |
Seconds to keep looking (every 0.5 s). |
delay |
Multiplier of the pause before pressing. |
comment |
Text added to the timeout message. |
thrw |
Throw when the element is not found in time; otherwise return quietly. |
thr0w |
Legacy switch: false also turns thrw off. |
emu |
1 — use full mouse emulation for this action, −1 — turn it off, 0 — leave the instance setting. |
public static void HeLongClick(this Instance instance, int x, int y, int holdMs = 3, double delay = 1, int emu = 0)
Extension method for Instance. source
Holds the left button at a point.
| Parameter | Description |
|---|---|
x |
X in the tab. |
y |
Y in the tab. |
holdMs |
Hold time in milliseconds. |
delay |
Multiplier of the pause before pressing. |
emu |
1 — use full mouse emulation for this action, −1 — turn it off, 0 — leave the instance setting. |
HeMultiClick
public static void HeMultiClick(this Instance instance, List<object> selectors)
Extension method for Instance. source
Clicks each element in turn with HeClick defaults.
HePeakRandom
public static void HePeakRandom(this Instance instance, object obj, int min = 1, int max = 10)
Extension method for Instance. source
Opens a drop-down by two long clicks and presses Down a random number of times.
| Parameter | Description |
|---|---|
obj |
Element: an HtmlElement; (value, "id") or (value, "name"); or (tag, attribute, pattern, mode, index) as in FindElementByAttribute. |
min |
Fewest presses. |
max |
Upper bound of presses (exclusive). |
HeSet
public static void HeSet(this Instance instance, object obj, string value, string method = "id", int deadline = 10, double delay = 1, string comment = "", bool thrw = true, bool thr0w = true, int emu = 0, string pathToScript = null)
Extension method for Instance. source
Waits for an input and enters value after a random pause of about 1.3–2 s × delay.
| Parameter | Description |
|---|---|
obj |
Element: an HtmlElement; (value, "id") or (value, "name"); or (tag, attribute, pattern, mode, index) as in FindElementByAttribute. |
value |
Text to enter. |
method |
For a 5-part selector: random picks a random match, last the last one; otherwise the index is used. |
deadline |
Seconds to keep looking (every 0.5 s). |
delay |
Multiplier of the pause. |
comment |
Text added to the timeout message. |
thrw |
Throw when the element is not found in time; otherwise return quietly. |
thr0w |
Legacy switch: false also turns thrw off. |
emu |
0 — set the value with ZennoPoster's full emulation; above 0 — click the field and type the text; below 0 — nothing is entered. |
pathToScript |
When set, appends the action and the element's XPath to this file. |
MousePOsCenter
public static int[] MousePOsCenter(this Instance instance, bool moveMouse = false)
Extension method for Instance. source
Turns on full mouse emulation and puts the cursor at the viewport centre.
| Parameter | Description |
|---|---|
moveMouse |
Move the cursor there instead of setting its position. |
Returns: The centre.
SaveCookies
public static string SaveCookies(this Instance instance)
Extension method for Instance. source
Returns the instance's cookies as saved by SaveCookie (through a temporary file).
ScrollDown
public static void ScrollDown(this Instance instance, int y = 420)
Extension method for Instance. source
Scrolls with the emulated mouse wheel.
| Parameter | Description |
|---|---|
y |
Wheel delta. |
SetTimeFromDb
public static void SetTimeFromDb(this Instance instance, IZennoPosterProjectModel project)
Extension method for Instance. source
Sets timezone emulation from the timezone JSON (timezoneOffset, timezoneName) of the account's _instance row; warns when there is none.
SwipeFromCenter
public static int[] SwipeFromCenter(this Instance instance, int distance, string direction = null, int[] bounds = null)
Extension method for Instance. source
Swipes from the viewport centre.
| Parameter | Description |
|---|---|
distance |
Swipe length, px. |
direction |
left, right, up or down; random when empty. |
bounds |
Keep the end point inside [x, y, width, height]. |
Returns: The end point.
SwipeImgToCenter
public static int[] SwipeImgToCenter(this Instance instance, string imgFile, int[] searchArea, float threshold = 0.95f, bool nativeSearch = false)
Extension method for Instance. source
Finds the image and swipes from it to the viewport centre.
| Parameter | Description |
|---|---|
imgFile |
Template image: a file path (.png, .jpg, .jpeg, .gif, .bmp, .webp) or Base64. |
searchArea |
Search area [x, y, width, height] in page pixels. |
threshold |
Required similarity, 0–1. |
nativeSearch |
Use FindImg instead of FindImgFast. |
Returns: The viewport centre.
TapCenter
public static int[] TapCenter(this Instance instance)
Extension method for Instance. source
Taps the viewport centre; returns the point.
TapImg
public static int[] TapImg(this Instance instance, string imgFile, int[] searchArea, float threshold = 0.99f, bool nativeSearch = false, int delay = 0)
Extension method for Instance. source
Finds the image and taps its centre (touch event).
| Parameter | Description |
|---|---|
imgFile |
Template image: a file path (.png, .jpg, .jpeg, .gif, .bmp, .webp) or Base64. |
searchArea |
Search area [x, y, width, height] in page pixels. |
threshold |
Required similarity, 0–1. |
nativeSearch |
Use FindImg instead of FindImgFast. |
delay |
Seconds to wait before tapping. |
Returns: The tapped point.
UpEmpty
public static void UpEmpty(this Instance instance)
Extension method for Instance. source
Launches Chromium without a profile folder.
UpFromFolder
public static void UpFromFolder(this Instance instance, string pathProfile, bool useProfile = false, BrowserType browserType = BrowserType.Chromium)
Extension method for Instance. source
Launches the browser with a profile folder.
| Parameter | Description |
|---|---|
pathProfile |
Profile folder. |
useProfile |
Apply the ZennoPoster profile too. |
browserType |
Browser to launch. |