JS Challenge: Help with an unofficial DIC Chrome Extension

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 9382 Views - Last Post: 13 February 2013 - 03:09 PM

#16 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: JS Challenge: Help with an unofficial DIC Chrome Extension

Posted 19 November 2012 - 03:14 PM

Quote

Are there any console errors? Remember that you'll have to open the popup, right-click->Inspect, then use the Console tab to reload the window to see them.


Wholly cal!! Nice...

Quote

Refused to load the script 'http://code.jquery.com/jquery.min.js' because it violates the following Content Security Policy directive: "script-src 'self' chrome-extension-resource:".


Funniest thing. I was looking at the security attribute "script-src" earlier, and I was like "Ahh... that's not important."
Was This Post Helpful? 0
  • +
  • -

#17 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,971
  • Joined: 08-June 10

Re: JS Challenge: Help with an unofficial DIC Chrome Extension

Posted 19 November 2012 - 03:35 PM

Ha!

I've actually included jquery in the bundle already, it's in the js folder.

Chrome debugging tools are the bee's knees.
Was This Post Helpful? 0
  • +
  • -

#18 StumpyMge  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 17-May 04

Re: JS Challenge: Help with an unofficial DIC Chrome Extension

Posted 12 February 2013 - 04:18 PM

View PostCurtis Rutland, on 19 November 2012 - 03:35 PM, said:

Ha!

I've actually included jquery in the bundle already, it's in the js folder.

Chrome debugging tools are the bee's knees.


Hey boss, for kicks I refactored the javascript I could find in there to use a library I wrote for monadic composition.

Let me know what you think and if you're interested in using this compositional library to simplify it or if you have any critiques. The interesting trick lives at the bottom of each file where I take the independent functions and compose them together into a cohesive workflows. (the then/or/success/failure functions are from the library I wrote)

This replaces dic/config/background js files (but could be broken back apart however you like as it's more modular now):

var getSettings = function (a) {
    return localStorage['settings'] ? success(JSON.parse(localStorage['settings'])) : failure(a);
};

var createSettings = function(a) {
    return success({
        subsUrl: 'http://www.dreamincode.net/forums/index.php?app=core&module=usercp&tab=forums&area=topicsubs',
        signinUrl: 'http://www.dreamincode.net/forums/index.php?&app=core&module=global&section=login&do=form',
        unreadSelector: 'img[src*="unread"]',
        linkSelector: 'a[href*="topic"]',
        recheckFrequency: 300000,
        otherPages: []});
};

var setBadge = function (text, color) {
    var textObj = { text: text };
    var colorObj = { color: color };
    chrome.browserAction.setBadgeBackgroundColor(colorObj);
    chrome.browserAction.setBadgeText(textObj);
};

var clearBadge = function (a) {
    setBadge("", [0, 255, 0, 0]);
    return success(a);
};

var setLoadingBadge = function (a) {
    setBadge("....", [255, 165, 0, 255]);
    return success(a);
};

var setUnreadBadge = function (a) {
    setBadge(a.length.toString(), [0, 255, 0, 255]);
    return success(a);
};

var setErrorBadge = function (a) {
    setBadge("error", [255, 0, 0, 255]);
    setTimeout(clearBadge, 3000);
    return success(a);
};

var saveSettings = function(a) {
    localStorage['settings'] = JSON.stringify(a);
    return success(a);
};

var createTab = function (a) {
    chrome.tabs.create({ 'url': a });
    return success(a);
};

var makeAjaxCall = function(a) {
    var b = $.ajax({url: a.subsUrl, dataType: 'html', cache: false, async: false});
    b.Settings = a;
    return b.state() !== 'resolved' && $(b.responseText).find('input#username').length > 0 ?
        failure(B)/>/> :
        success(B)/>/>;
};

var getUnread = function(a) {
    return success($(a.responseText).find(a.Settings.unreadSelector).parent().parent());
};

var log = function(a) { console.log(a); return success(a); };

var initializeSettings = getSettings.or(createSettings.then(saveSettings));
var getUnreadSubscriptions = setLoadingBadge.then(makeAjaxCall).then(clearBadge).then(getUnread);
var handleResults = setUnreadBadge.or(setErrorBadge.then(log));

var machine = initializeSettings.then(getUnreadSubscriptions).then(handleResults);

machine();
setInterval(machine, 300000);



This replaces the popup.js:

var makeLinksOpenInTabs = function() {
    $('a').on('click', function () {
        if ($(this).attr('href') == '#') return;
        clearBadge();
        createTab($(this).attr('href'));
    });
};

var setOpenAllLink = function(links) {
    $('#open-all').on('click', function () {
        clearBadge();
        for (var i = 0; i < links.length; i++) {
            createTab(links[i].attr('href'));
        }
    });
};

var showLoader = function(a) {
    $('#loader').show();
    return success(a);
};

var hideLoader = function(a) {
    $('#loader').hide();
    return success(a);
};

var showError = function(a) {
    $('#alert-error').show();
    return success(a);
};

var showNoEntries = function(a) {
    $('#alert-info').show();
    return success(a);
};

var showEntries = function(a) {
    if (a.length < 1) return failure(a);
    $('#entries').show();
    var list = $('#entries').find('ul');
    var links = new Array();
    $(a.responseText).find(a.Settings.linkSelector).each(function () {
        list.append($('<li></li>').append($(this)));
        links.push($(this));
    });
    list.append($('<li class="divider"></li>'));
    list.append($('<li><a href="#" id="open-all">Open All</a></li>'));
    setOpenAllLink(links);

    return success(a);
};

$(document).on('ready', function () {
    var handleError = showError.then(setErrorBadge).then(hideLoader).then(failure);

    initializeSettings
        .then(showLoader)
        .then(getUnreadSubscriptions.or(handleError))
        .then(hideLoader)
        .then(showEntries.or(showNoEntries))();
});



Was This Post Helpful? 1
  • +
  • -

#19 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,971
  • Joined: 08-June 10

Re: JS Challenge: Help with an unofficial DIC Chrome Extension

Posted 12 February 2013 - 04:59 PM

That's very interesting. I'll have to look at that in more depth when I finish the work project I'm on now. Haven't had a lot of time for personal projects lately. I'm a bit surprised that this didn't get very much traction; I'd hoped that more people would get involved.
Was This Post Helpful? 0
  • +
  • -

#20 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: JS Challenge: Help with an unofficial DIC Chrome Extension

Posted 12 February 2013 - 05:36 PM

Does anyone know / remember one called DIC Stretcher, that made the forum post width upscale to size of the window. It may also got ride of the two column on the righthand side.
Was This Post Helpful? 0
  • +
  • -

#21 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: JS Challenge: Help with an unofficial DIC Chrome Extension

Posted 13 February 2013 - 12:13 PM

View PostAdamSpeight2008, on 13 February 2013 - 12:36 AM, said:

Does anyone know / remember one called DIC Stretcher, that made the forum post width upscale to size of the window. It may also got ride of the two column on the righthand side.

I have a Userscript called that I made ages ago. Doesn't remove the columns though.

http://atli.advefir..../dic_stretcher/
Was This Post Helpful? 0
  • +
  • -

#22 StumpyMge  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 17-May 04

Re: JS Challenge: Help with an unofficial DIC Chrome Extension

Posted 13 February 2013 - 02:32 PM

View PostCurtis Rutland, on 19 November 2012 - 03:09 PM, said:

Are there any console errors? Remember that you'll have to open the popup, right-click->Inspect, then use the Console tab to reload the window to see them.


Nope it works fine, I'm using it now. The key like I said is my Machinad.js library you can grab here:
https://github.com/JimmyHoffa/Machinad
Was This Post Helpful? 0
  • +
  • -

#23 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,971
  • Joined: 08-June 10

Re: JS Challenge: Help with an unofficial DIC Chrome Extension

Posted 13 February 2013 - 03:09 PM

That wasn't in response to you (couldn't be by the date). It was in response to another member that specifically had problems using the extension.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2