Skip to content

List of Common User Scripts

marklieberman edited this page May 31, 2018 · 7 revisions

This page is a collection of frequently requested user scripts. It can serve as a temporary repository until a more permanent solution for sharing user scripts can be implemented.

Substitute 'commandOpenLinkInNewBackgroundTab' as desired for any user script that opens a link in a foreground tab.

Go to URL

Substitute your home page for a 'Go Home' command.

executeInBackground(() => {
   getActiveTab(tab => browser.tabs.update(tab.id, { url: 'http://www.reddit.com' }));
});

Open a URL in a New Foreground Tab

data.element.linkHref = 'http://www.reddit.com';
executeInBackground(data => commandOpenLinkInNewForegroundTab(data), [ data ]);

Maximize/Restore Window

executeInBackground(() => {
    browser.windows.getCurrent().then(win => {
        if (win.state === 'maximized') {
            browser.windows.update(win.id, { state: 'normal' });
        } else {
            browser.windows.update(win.id, { state: 'maximized' });
        }
    })
})

Copy URL to Clipboard

let src = data.element && data.element.linkHref;
if (src) {
    // Create a text area with the value we want to copy.
    let textarea = document.createElement("textarea");
    textarea.value = src;
    
    // Style textarea to make it fairly invisible.
    // Copy doesn't work when display: none.
    textarea.style.maxHeight = '2em';
    textarea.style.maxWidth = '2em';
    textarea.style.position = 'fixed';
    textarea.style.top = 0;
    textarea.style.left = 0;
    textarea.style.padding = 0;
    textarea.style.border = 'none';
    textarea.style.outline = 'none';
    textarea.style.boxShadow = 'none';
    textarea.style.background = 'transparent';
    
    // Append the textarea, copy its value, then remove it.
    document.body.appendChild(textarea);
    textarea.select();
    document.execCommand("copy");
    textarea.remove();
    
    // Update status text for gesture.
    setStatus('Copied: ' + src);
}

Hybrid: Open Link/New Tab

if (data.element.linkHref) { 
    executeInBackground(data => commandOpenLinkInNewForegroundTab(data), [ data ]); 
} else { 
    executeInBackground(data => commandNewTab(data), [ data ]); 
}

Hybrid: Open Link/New Window

if (data.element.linkHref) { 
    executeInBackground(data => commandOpenLinkInNewWindow(data), [ data ]); 
} else { 
    executeInBackground(data => commandNewWindow(data), [ data ]); 
}

Hybrid: Search Text/Open Link in a New Foreground Tab

// Note: in this script we replace the linkHref property on the event data so we can re-use the built in open link command.

let selectedText = window.getSelection().toString();
if (selectedText) {
    // Check if the selection looks like a hyperlink.
    if (/\s*https?:\/\//.test(selectedText)) {
        // Open the link in a new foreground tab.
        data.element.linkHref = selectedText.trim();
        executeInBackground(data => commandOpenLinkInNewForegroundTab(data), [ data ]);
    } else {
        // Search for the selected text in a new foreground tab.
        data.element.linkHref = 'https://encrypted.google.com/search?q=' + encodeURIComponent(selectedText);
        executeInBackground(data => commandOpenLinkInNewForegroundTab(data), [ data ]);
    }
} else
if (data.element.linkHref) {
    // Open the link in a new foreground tab.
    executeInBackground(data => commandOpenLinkInNewForegroundTab(data), [ data ]);
}

Collapse all nodes in the tabs tree of the Tree Style Tabs addon

User script by orthoceros.

//First, gather any needed page context information in-sync in the page JS environment:
var pageContext = data; //page-level context info provided by FoxyGestures; not really needed in case of this "collapse all tabs" command.
    
//Forward page context information to the browser background JS environment:
executeInBackground(pageContext => {(async function collapseAllTabsInActiveWindow() {
    //Initialize: collect active window and get its tree structure from the TST API:
        //console.log(pageContext);
        let activeWindow = await browser.windows.getCurrent({populate: false});
        let treeInfo4activeWindow = await browser.runtime.sendMessage('[email protected]', {
          type: 'get-tree',
          window: activeWindow.id
        });
        //console.log(treeInfo4activeWindow)
    //Define tree traversal function:
        function collapseRecursivelyFromLastToFirst(aTSTtabsArray) {
            for (var ti = aTSTtabsArray.length-1; ti >=0; ti -= 1) {
                //traverse descendants first:
                    if(aTSTtabsArray[ti].children && aTSTtabsArray[ti].children.length>0) {
                        collapseRecursivelyFromLastToFirst(aTSTtabsArray[ti].children);
                    }
                //Collapse current node, if not already collapsed:
                    let bAlreadyCollapsed = aTSTtabsArray[ti].states.includes("subtree-collapsed");
                    if(!bAlreadyCollapsed) {
                        //console.log("collapsing tab "+ aTSTtabsArray[ti].id);
                        browser.runtime.sendMessage('[email protected]', {
                    	    type: 'collapse-tree',
                    		tab:  aTSTtabsArray[ti].id
                    	});
                    }
            }
        }
    //Start traversal:
        collapseRecursivelyFromLastToFirst(treeInfo4activeWindow);
})()}, [pageContext]); //executeInBackground and call the async function there.

Enlarge or Reduce Image Size

Set SCALE to a value less then 1 for reduce and greater than 1 for enlarge.

const SCALE = 1.5;

var node = mouseDown.target;
if (node instanceof HTMLImageElement) {
    if (!node.hasAttribute("width")) {
        node.setAttribute("width", node.naturalWidth);
    }
    if (!node.hasAttribute("height")) {
        node.setAttribute("height", node.naturalHeight);
    }
    
    // Remove styles that override the width and height attributes.
    if (node.style.width !== '') {
        node.style.width = '';
    }
    if (node.style.maxWidth !== '') {
        node.style.maxWidth = '';
    }
    if (node.style.height !== '') {
        node.style.height = '';
    }
    if (node.style.maxHeight !== '') {
        node.style.maxHeight = '';
    }
    
    node.width = node.width * SCALE;
    node.height = node.height * SCALE;
    
}

// Allow repetition when used with wheel or chord gestures.
var result = { repeat: true };
result;