Skip to content

Instantly share code, notes, and snippets.

@elliz
Last active September 22, 2021 03:08
Show Gist options
  • Save elliz/0d9446ccec5cd4159c01f93992a82fff to your computer and use it in GitHub Desktop.
Save elliz/0d9446ccec5cd4159c01f93992a82fff to your computer and use it in GitHub Desktop.
Optus Huawei E5186 Domain Name Filter TamperMonkey script
// ==UserScript==
// @name Optus - Domain Name Filter
// @namespace http://tampermonkey.net/
// @version 0.2
// @description try to take over the world!
// @author You
// @match http://192.168.8.1/html/urlfilter.html
// @grant GM_addStyle
// ==/UserScript==
(function() {
'use strict';
addButton(".apply_button", 'btn-youtube', 'YouTube', function(){toggleUrl("youtube.com")});
addButton(".apply_button", 'btn-netflix', 'Netflix', function(){toggleUrl("netflix.com")});
GM_addStyle ( `
.btn-fancy{
margin: 0 2px 1.5em;
font-size: 100%;
padding: .5em 1em;
color: rgba(0,0,0,.8);
border: transparent;
background-color: #e6e6e6;
text-decoration: none;
border-radius: 2px;
display: inline-block;
line-height: normal;
white-space: nowrap;
vertical-align: middle;
text-align: center;
cursor: pointer;
user-select: none;
box-sizing: border-box;
}
.btn-fancy:hover {
background-image: linear-gradient(to bottom, transparent, rgba(0,0,0,.05) 40%, rgba(0,0,0,.1));
}
.btn-fancy:active {
background-image: linear-gradient(to top, transparent,rgba(0,0,0,.05) 40%, rgba(0,0,0,.1));
}
`);
function addButton(parentQuerySelector, id, text, onclick) {
console.log('Tampermonkey Script: adding button');
let button = document.createElement('button');
document.querySelector(parentQuerySelector).appendChild(button);
button.innerHTML = text;
button.onclick = onclick;
button.className = "btn btn-fancy";
button.id = id;
return button;
}
function toggleUrl(url) {
console.log(`toggling value for ${url}`);
var rowIndex = findOrCreateUrl(url);
toggleValue(rowIndex);
applyChanges();
};
function findOrCreateUrl(url, depth = 0)
{
console.log({depth});
var items = getDomainNames();
let foundIndex = items.findIndex(x => x.url === url);
if (foundIndex === -1)
{
console.log("did not find value", foundIndex, url);
addUrl(url);
findOrCreateUrl(url, depth+1);
}
return foundIndex;
};
function getDomainNames(){
const dataRowSelector = "table#service_list tr.user_add_line";
let dataRows = Array.from(document.querySelectorAll(dataRowSelector));
return dataRows.map(r => {
return {
url : r.cells[0].innerText,
status : r.cells[1].innerText,
editBtn : r.cells[2].querySelector(".button_edit_list")
}});
}
function addUrl(url)
{
let addBtn = document.getElementById("add_item");
addBtn.click();
let input = document.getElementById("input_domain_name");
let okLink = document.getElementById("add_item_ok");
input.value = url;
okLink.click();
let saveConfirmationOk = document.GetElementById("pop_confirm");
saveConfirmationOk.click();
};
function toggleValue(rowIndex){
// find edit button
var items = getDomainNames();
var itemToEdit = items[rowIndex];
console.log("toggling value for", itemToEdit);
let editBtn = itemToEdit.editBtn;
editBtn.click();
console.log(editBtn);
let onOff = document.getElementById("select_status");
let okLink = document.getElementById("edit_item_ok");
onOff.value = (itemToEdit.status === "On") ? 0 : 1;
okLink.click();
}
function applyChanges() {
let applyBtn = document.getElementById("apply");
applyBtn.click();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment