Isaac
Isaac

Reputation: 151

How can I disable javascript for the page that's being opened?

I was up till 1 am last night trying to find an example of how to do this. My theory is that I'd write a function that would comment out all javascript.

The second option would be to add the url to the list of javascript settings.

Right now my extension is very simple:

function linkOnClick(info, tab) {
    window.open(info.linkUrl)
}

chrome.contextMenus.create(
{title: "Load with no Javascript", contexts:["link"], onclick: linkOnClick});

This is my first extension and I'm kind of lost.

edit: let me know if I should also post the manifest.json.

Upvotes: 3

Views: 1026

Answers (1)

Isaac
Isaac

Reputation: 151

edit: I can't mark this as solved for 2 days (why? who knows.), so I'll probably not remember to mark this as solved. So accept this as the official making: SOLVED.

chrome.contentSettings.javascript.set is the thing that disables javascript.

Here's the part that disables javascript. (Google, here's what an actual example should look like):

    chrome.contentSettings.javascript.set(
        {'primaryPattern':AnyDomainName, /*this is a string with the domain*/
        'setting': "block", /* block the domain. Can be switched to "allow" */
        'scope':'regular'}, /*it's either regular or incognito*/
        function(){ 
            /*optional action you want to 
            take place AFTER something's been blocked'*/    
            });

Here's the script I used to import into my json script for my chrome extension.

var link=""
var pattern=""

function linkOnClick(info, tab) {
    r = /:\/\/(.[^/]+)/;
    link=info.linkUrl
    pattern="http://"+link.match(r)[1]+"/*"

    chrome.contentSettings.javascript.set(
        {'primaryPattern':pattern,
        'setting': "block",
        'scope':'regular'},
        function(){

        window.open(link)

        });
    }

chrome.contextMenus.create({title: "Load with no Javascript", contexts:["link"], onclick: linkOnClick});

I couldn't tell how any of this worked by reading the developer.chrome.com page! They really need add complete working examples or allow a way for users to add examples. I can't even use it. The git hub link is what saved me.

Upvotes: 2

Related Questions