Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ANSH3LL authored Apr 20, 2021
1 parent 5c8029d commit c70c5a4
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 0 deletions.
Binary file added icons/icon-128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/icon-96.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "Picture",
"version": "0.1",
"manifest_version": 2,
"description": "Redirects all instagram URLs to smihub.com or picuki.com per user choice",

"icons": {
"48": "icons/icon-48.png",
"96": "icons/icon-96.png",
"128": "icons/icon-128.png"
},

"browser_action": {
"default_title": "Picture",
"default_icon": "icons/icon-32.png",
"default_popup": "popup/options.html"
},

"browser_specific_settings": {
"gecko": {
"id": "@ansh3ll.picture"
}
},

"background": {
"scripts": ["picture.js"]
},

"permissions": [
"storage",
"webRequest",
"webRequestBlocking",
"*://*.instagram.com/*"
]
}
70 changes: 70 additions & 0 deletions picture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//smihub
const smihub = 'https://smihub.com/';
const postPrefixS = 'https://smihub.com/c/';
const profilePrefixS = 'https://smihub.com/v/';
//picuki
const picuki = 'https://www.picuki.com/';
const postPrefixP = 'https://www.picuki.com/media/';
const profilePrefixP = 'https://www.picuki.com/profile/';
//regex
const postRegex = /(?<!^https\:\/\/www\.google\.com\/imgres\?imgurl.*)(?:https?\:\/\/)?(?:www\.)?(instagram\.com|instagr\.am|next\=)(?:\/p|\/reel)\/([a-zA-Z._0-9-]+)/i;
const profileRegex = /(?<!^https\:\/\/www\.google\.com\/imgres\?imgurl.*)(?:https?\:\/\/)?(?:www\.)?(?<!help\.|api\.|business\.|about\.|lookaside\.)(instagram\.com|instagr\.am|next\=)\/(?!accounts|explore|developer|reel)([a-zA-Z._0-9]{3,})/i;

var filter = {urls: ["*://*.instagram.com/*"]};

function getUsername(igurl) {
url = decodeURIComponent(igurl);
return url.match(profileRegex)[2];
}

function getMediaId(igurl) {
mediaId = BigInt(0);
url = decodeURIComponent(igurl);
shortcode = url.match(instaPost)[2].split('');
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
shortcode.forEach((letter) => {
mediaId = BigInt((mediaId * 64n)) + BigInt(alphabet.indexOf(letter));
});
return mediaId.toString().split('').reverse().join('');
}

function setPrefix(result) {
prefix = result.handler;
if(prefix == 1 || prefix == 2) {
document.igRequestHandler = prefix;
if(!browser.webRequest.onBeforeRequest.hasListener(igRedirect)) {
browser.webRequest.onBeforeRequest.addListener(igRedirect, filter, ['blocking']);
}
} else if(browser.webRequest.onBeforeRequest.hasListener(igRedirect)) {
browser.webRequest.onBeforeRequest.removeListener(igRedirect);
}
}

function loadPreferences(changes, areaName) {
pref = browser.storage.local.get('handler');
pref.then((result) => {setPrefix(result);}, (error) => {});
}

function igRedirect(details) {
if(document.igRequestHandler == 1) {
redirectTo = smihub;
postPrefix = postPrefixS;
profilePrefix = profilePrefixS;
} else if(document.igRequestHandler == 2) {
redirectTo = picuki;
postPrefix = postPrefixP;
profilePrefix = profilePrefixP;
}
//
if(postRegex.test(decodeURIComponent(details.url))) {
url = postPrefix + getMediaId(details.url);
} else if(profileRegex.test(decodeURIComponent(details.url))) {
url = profilePrefix + getUsername(details.url);
} else {
return {redirectUrl: redirectTo};
}
return {redirectUrl: url};
}

browser.storage.onChanged.addListener(loadPreferences);
browser.webRequest.onBeforeRequest.addListener(igRedirect, filter, ['blocking']);
14 changes: 14 additions & 0 deletions popup/options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<p id="status">Status: Using smihub.com</p>
<button id="smihub">Use smihub.com</button>
<button id="picuki">Use picuki.com</button>
<button id="disabled">Disable Picture</button>
<p>Icon made by <a href="https://www.freepik.com" title="Freepik">Freepik</a> from <a href="https://www.flaticon.com/" title="Flaticon">www.flaticon.com</a></p>
<script src="options.js"></script>
</body>
</html>
43 changes: 43 additions & 0 deletions popup/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function savePreferences(pref) {
browser.storage.local.set({handler: pref});
}

function loadPreferences() {
pref = browser.storage.local.get('handler');
pref.then((result) => {updateUi(result);}, (error) => {displayError();});
}

function updateUi(result) {
pref = result.handler;
if(pref == 1) {
document.getElementById('status').innerHTML = 'Status: Using smihub.com';
} else if(pref == 2) {
document.getElementById('status').innerHTML = 'Status: Using picuki.com';
} else {
document.getElementById('status').innerHTML = 'Status: Disabled';
}
}

function displayError() {
document.getElementById('status').innerHTML = 'Status: Error resolving handler';
}

function setSmihub() {
savePreferences(1);
loadPreferences();
}

function setPicuki() {
savePreferences(2);
loadPreferences();
}

function setDisabled() {
savePreferences(3);
loadPreferences();
}

document.addEventListener('DOMContentLoaded', loadPreferences);
document.getElementById('smihub').addEventListener('click', setSmihub);
document.getElementById('picuki').addEventListener('click', setPicuki);
document.getElementById('disabled').addEventListener('click', setDisabled);

0 comments on commit c70c5a4

Please sign in to comment.