Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name MAB file picker
- // @version 1.0
- // @match https://*.myairbridge.com/*
- // @run-at document-idle
- // ==/UserScript==
- (function() {
- "use strict";
- function main() {
- "use strict";
- const api = (function() {
- const crc32 = (function() {
- const table = new Uint32Array(256);
- for (let i = 0; i < 256; ++i) {
- let crc = i;
- for (let j = 0; j < 8; ++j)
- crc = (crc & 1) ? 3988292384 ^ crc >>> 1 : crc >>> 1;
- table[i] = crc;
- }
- return function crc32(s) {
- let crc = -1;
- for (let i = 0; i < s.length; ++i)
- crc = crc >>> 8 ^ table[(crc ^ s.charCodeAt(i)) & 255];
- return (crc ^ -1) >>> 0;
- }
- })();
- function request(path, action, data) {
- const req = {
- r: JSON.stringify({
- action,
- lang: "en", // unneeded
- data,
- token: "" // unneeded
- }),
- p: "json",
- v: "2.0",
- s: "" // unneeded
- };
- req.s = crc32(
- unescape(encodeURIComponent([req.r, req.p, req.v].join("|")))
- ).toString(16);
- return $.post(path, req, undefined, "json");
- }
- return {
- getUploadDetail: (id) => request(
- "/api2/common/",
- "GetUploadDetail",
- {
- id,
- login_token: "" // unneeded
- }
- ),
- createTicket: (upload, files) => request(
- "/download/api/",
- "CreateTicket",
- {
- files,
- src: 1, // unneeded
- token: "", // unneeded
- upload // unneeded
- }
- ),
- createDownload: (content_id, channel, ticket) => request(
- "/download/api/",
- "createDownload",
- {
- channel,
- content_id,
- download_type: "file",
- login_token: "", // unneeded
- source: "html5_beta", // unneeded
- ticket
- }
- ),
- createDownloadPackage: (content_id, files, ticket) => request(
- "/download/api/",
- "CreateDownloadPackage",
- {
- content_id,
- files,
- login_token: "", // unneeded
- source: "html5_beta", // unneeded
- ticket
- }
- )
- };
- })();
- api.getUploadDetail(
- window.location.href.match(/\/link\/([^/]+)/)[1]
- ).then(({data: {id, channel, files}}) => {
- document.head.insertAdjacentHTML(
- "beforeend",
- `<style type="text/css">
- .base_download > .button:not(.dlbutton) {
- display: none !important;
- }
- </style>`
- );
- const selected = new Set();
- if (files.length > 1) {
- document.head.insertAdjacentHTML(
- "beforeend",
- `<style type="text/css">
- .file > .name {
- cursor: pointer !important;
- }
- .file.selected,
- .file.selected > .name > .fade {
- background: #f5f5f5 !important;
- }
- .file.selected > .name,
- .file.selected > .control {
- color: #359bce !important;
- font-weight: bold !important;
- }
- </style>`
- );
- $(".base_download > .files").on("click", ".file", (e) => {
- const el = $(e.currentTarget);
- const file_id = files[el.index()].id;
- if (!el.hasClass("selected")) {
- el.addClass("selected");
- selected.add(file_id);
- } else {
- el.removeClass("selected");
- selected.delete(file_id);
- }
- });
- }
- const button = $("<a class=\"button dlbutton\">Download</a>");
- button.on("click", () => {
- const ids = (selected.size === 0) ? files.map((f) => f.id)
- : [...selected];
- api.createTicket(id, ids).then(({data: {ticket}}) => {
- const dl = (ids.length > 1) ? api.createDownloadPackage(id, ids, ticket)
- : api.createDownload(ids[0], channel, ticket);
- dl.then(({data: {link}}) => window.location.assign(link));
- });
- });
- $(".base_download > .button").first().before(button);
- });
- }
- function inject(fn) {
- const script = document.createElement("script");
- script.type = "text/javascript";
- script.textContent = "(" + fn.toString() + ")();";
- document.documentElement.appendChild(script);
- document.documentElement.removeChild(script);
- }
- // Wait for a download button to become visible before injecting (to ensure that
- // the page is fully constructed and the upload is complete, etc).
- (new MutationObserver((ms, ob) => {
- const isDownloadButton = (el) => (
- el.nodeType === 1 &&
- el.classList.contains("button") &&
- /^Download/.test(el.textContent) &&
- el.style.display === "inline-block"
- );
- for (let i = 0; i < ms.length; ++i) {
- if (isDownloadButton(ms[i].target)) {
- ob.disconnect();
- inject(main);
- return;
- }
- }
- })).observe(
- document.body,
- {subtree: true, attributes: true, attributeFilter: ["style"]}
- );
- })();
Add Comment
Please, Sign In to add comment