Toggling click handler using waitForKeyElements - greasemonkey

I recently discovered, waitForKeyElements. An amazing utility from BrockA.
I'm using the userscript below in Tampermonkey to automatically accept chats from SalesForce's LiveAgent.
Works great, my question is if there's a way to add a button or a link to toggle between it, without having to flip the disable & enable switch on Tampermonkey, and then refreshing the page?
Any help is greatly appreciated since I've spent a few days looking for an answer, and very little to show for. Thanks in advance!!
// ==UserScript==
// #name AutoAccept Chats
// #version 0.1
// #author Me
// #include https://*.ladesk.com/agent/*
// #require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// #require https://gist.github.com/raw/2625891/waitForKeyElements.js
// #grant GM_addStyle
// ==/UserScript==
'use strict';
waitForKeyElements (
"div.TicketNotificationWindowIn",
AnswerChat
);
function AnswerChat (jNode) {
document.getElementsByClassName("ImLeButtonImLeButtonMainOut TicketNotificationAcceptButton")[0].click();
}
This is what I had which didn't work, but might help explain my question.
var turnsOff = 1;
function doToggle() {
if (turnsOff == 1) {
turnsOff = 2;
}else{
turnsOff = 1;
}
ToggleState();
}
function ToggleState () {
if (turnsOff == 1) {
waitForKeyElements (
"div.TicketNotificationWindowIn",
AnswerChat
);
}else{
console.log('not answered');
}
}

Related

Where do I execute my on-first-start functions in my Electron app?

I am using Electron 9 and I have a main process and a single render process. On the first start of my application I would like to execute some code which is not executed on the second run.
Does Electron have a dedicated location where I should do this? Any help is highly appreciated!
Use app.getPath('userData') - it's dedicated location for your apps data for current user (eg. in windows it will point to something like AppData/Roaming/app-name/)
At startup use:
app.on('ready', () => {
const firstTimeFilePath = path.resolve(app.getPath('userData'), '.first-time-huh');
let isFirstTime;
try {
fs.closeSync(fs.openSync(firstTimeFilePath, 'wx'));
isFirstTime = true;
} catch(e) {
if (e.code === 'EEXIST') {
isFirstTime = false;
} else {
// something gone wrong
throw e;
}
}
// ...
});
Profit!
https://nodejs.org/api/fs.html#fs_file_system_flags - why use wx flag
https://nodejs.org/api/fs.html#fs_fs_opensync_path_flags_mode - fs.openSync()
https://www.electronjs.org/docs/api/app#appgetpathname - app.getPath()
If you want to write out default preferences in the first run and read them in the next runs, try this:
import defaults from './default_preferences.json'; // will work for plain js objects too
let prefs = defaultPrefs;
app.on('ready', () => {
const prefsPath = path.resolve(app.getPath('userData'), 'prefs.json');
let isFirstTime;
try {
fs.writeFileSync(prefsPath, JSON.stringify(defaultPrefs), { flag: 'wx' });
isFirstTime = true;
} catch (e) {
if (e.code === 'EEXIST') {
// slight posibility of races, you can eleminate it by using `singleInstanceLock` or waiting loop for `write` flag
prefs = require(prefsPath);
isFirstTime = false;
} else {
// something gone wrong
throw e;
}
}
...
});

NetSuite SuiteScript 2.0 disable field based on checkbox

I apologize if this is a dumb question, but I am new to NetSuite, and have noticed that their documentation is absolutely ridiculously horrifyingly and atrociously disgusting. All humor and bitterness aside though, I can't find the details that should exists in SuiteAnswers. I can find the type Field or Record, but it doesn't show me the options available for those types. It just shows what methods to call to return a field or record.
So I have it on the fieldChanged event as the training specifies, and below is what I have.
function fieldChanged(context) {
debugger;
var customer = context.currentRecord
if (context.fieldId == 'custentity_apply_coupon') {
var field = record.getField("custentity_apply_coupon");
if (record.getValue("custentity_apply_coupon") == true) {
reord.getField("custentity_coupon_code").isDisabled = false;
}
else{
reord.getField("custentity_coupon_code").isDisabled = true;
}
field.isDisabled = false;
}
}
Turns out that, and I never found this in the documentation, that once you get the field from currentRecord.currentRecord, you can set it to disabled via field.isDisabled. Took me forever to find out that isDisabled was a property of field, and then took a complete guess to see that isDisabled was a get/set call for ClientSide Scripts. Below is the code that ended up working.
function fieldChanged(scriptContext) {
var customer = scriptContext.currentRecord;
if(scriptContext.fieldId == "custentity_sdr_apply_coupon"){
debugger;
var field = customer.getField("custentity_sdr_coupon_code");
field.isDisabled = !customer.getValue(scriptContext.fieldId);
if(field.isDisabled){
customer.setValue(field.id, "");
}
}
}
I hope this will help.
function fieldChanged(context) {
var currentRecord = context.currentRecord;
var approvalChkBox = currentRecord.getValue({
fieldId: 'supervisorapproval'
});
var memoField = currentRecord.getField("memo");
if (approvalChkBox)
memoField.isDisabled = true;
else
memoField.isDisabled = false;
}
Thats a good question, this is the simplest solution you are looking for. use getValue method and isDisabled to meet this requirement. the code is self explanatory. Good Luck.
function fieldChanged(context) {
var record = context.currentRecord;
var fieldname = context.fieldId;
var changedValue = record.getValue(fieldname); //getValue method is the key here
var couponid = record.getField('custentity_kld_coupon_code');
if (fieldname == 'custentity_kld_apply_coupon' && changedValue == true) {
couponid.isDisabled = false; //isDisabled helps you to enable or disable a field
} else {
couponid.isDisabled = true;
}
}
Totally agree. I think the SuiteScript 2.0 Student Guide could've been more helpful if they included a preview of their codes along the way.
For anyone else who is still following along, this code below worked for me. Thanks for everyone else that contributed in this post. Used your codes to create this too. I also included some other codes from the previous exercises (i.e. displaying a message when entering 'x' into the coupon code).
/**
* #NScriptType ClientScript
* #NApiVersion 2.0
*/
define([],
function() {
function fieldChanged (context) {
var customer = context.currentRecord;
if(context.fieldId = 'custentity_sdr_apply_coupon') {
var check = customer.getValue('custentity_sdr_apply_coupon');
var code = customer.getField('custentity_sdr_coupon_code');
if (check == true){
code.isDisabled = false;
} else {
code.isDisabled = true;
}
}
}
function saveRecord(context) {
var customer = context.currentRecord;
var empCode = customer.getValue('custentity_sdr_coupon_code')
if(empCode == 'x') {
alert('Invalid code value. Please try again');
return false;
}
return true;
}
return {
fieldChanged: fieldChanged,
saveRecord: saveRecord,
};
});
var objRec_Curr = scriptContext.currentRecord;
var TransferType = objRec_Curr.getCurrentSublistValue({sublistId:'xxxxxxxxxx', fieldId : 'xxxxxxxxxxxx'});
if(TransferType == 'ABC')
eval("nlapiDisableLineItemField('custpage_sublist_out', 'custpage_out_transfer_location', true)");
else
eval("nlapiDisableLineItemField('custpage_sublist_out', 'custpage_out_transfer_location', false)");

Debug assertion error on printing

I have a simple text editor that I created in Visual Studio 2010 Professional Edition. Basically I modified the MFC MDI program automatically generated by the VS2010 wizard. The problem is that when i print, it gives me a debug assertion error in viewrich.cpp line 294. I have not modified anything in the code to do with printing, though it could be something wrong with how i used Rich Edit. This is all the information I have. Thanks in advance.
Viewrich.cpp
BOOL CRichEditView::PaginateTo(CDC* pDC, CPrintInfo* pInfo)
// attempts pagination to pInfo->m_nCurPage, TRUE == success
{
ASSERT_VALID(this);
ASSERT_VALID(pDC);
CRect rectSave = pInfo->m_rectDraw;
UINT nPageSave = pInfo->m_nCurPage;
ASSERT(nPageSave > 1); // LINE 294
ASSERT(nPageSave >= (UINT)m_aPageStart.GetSize());
VERIFY(pDC->SaveDC() != 0);
pDC->IntersectClipRect(0, 0, 0, 0);
pInfo->m_nCurPage = (int)m_aPageStart.GetSize();
while (pInfo->m_nCurPage < nPageSave)
{
ASSERT(pInfo->m_nCurPage == (UINT)m_aPageStart.GetSize());
OnPrepareDC(pDC, pInfo);
ASSERT(pInfo->m_bContinuePrinting);
pInfo->m_rectDraw.SetRect(0, 0,
pDC->GetDeviceCaps(HORZRES), pDC->GetDeviceCaps(VERTRES));
pDC->DPtoLP(&pInfo->m_rectDraw);
OnPrint(pDC, pInfo);
if (pInfo->m_nCurPage == (UINT)m_aPageStart.GetSize())
break;
++pInfo->m_nCurPage;
}
BOOL bResult = pInfo->m_nCurPage == nPageSave;
pDC->RestoreDC(-1);
pInfo->m_nCurPage = nPageSave;
pInfo->m_rectDraw = rectSave;
ASSERT_VALID(this);
return bResult;
}
EmergenceView.cpp
IMPLEMENT_DYNCREATE(CEmergenceView, CRichEditView)
BEGIN_MESSAGE_MAP(CEmergenceView, CRichEditView)
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, &CRichEditView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CRichEditView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CEmergenceView::OnFilePrintPreview)
ON_WM_CONTEXTMENU()
ON_WM_RBUTTONUP()
ON_COMMAND(ID_MUTATE_GROUP, &CEmergenceView::OnMutateGroup)
ON_UPDATE_COMMAND_UI(ID_MUTATE_GROUP, &CEmergenceView::OnUpdateMutateGroup)
ON_COMMAND(ID_MUTATE_RANDOMISE, &CEmergenceView::OnMutateRandomise)
ON_UPDATE_COMMAND_UI(ID_MUTATE_RANDOMISE, &CEmergenceView::OnUpdateMutateRandomise)
ON_COMMAND(ID_HELP_STATISTICS, &CEmergenceView::OnHelpStatistics)
ON_UPDATE_COMMAND_UI(ID_HELP_STATISTICS, &CEmergenceView::OnUpdateHelpStatistics)
ON_COMMAND(ID_MUTATE_POETRIZE, &CEmergenceView::OnMutatePoetrize)
ON_COMMAND(ID_EDIT_SELECTALL, &CEmergenceView::OnEditSelectall)
END_MESSAGE_MAP()
// CEmergenceView construction/destruction
CEmergenceView::CEmergenceView()
{
// TODO: add construction code here
}
CEmergenceView::~CEmergenceView()
{
}
BOOL CEmergenceView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CRichEditView::PreCreateWindow(cs);
}
// CEmergenceView drawing
void CEmergenceView::OnDraw(CDC* /*pDC*/)
{
CEmergenceDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: add draw code for native data here
}
// CEmergenceView printing
void CEmergenceView::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
AFXPrintPreview(this);
#endif
}
BOOL CEmergenceView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CEmergenceView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CEmergenceView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
The ASSERT says it all:
UINT nPageSave = pInfo->m_nCurPage;
ASSERT(nPageSave > 1);
This is a value for the current page to print/paginate. It is set to 1 in CPrintInfo's constructor. But something changed it and made it 0 or negative. Usually this value is completely controlled by the RTF printout. So you must done something that manipulates it.
You have to set the Minimum page and the Maximum page value (SetMinPage and SetMaxPage) in CPrintInfo.

YUI3 scrollView and mousewheel

I'm starting to work only with YUI3. I include component scrollView, but it did not work mousewheel event, in the options I have not found how to turn on it. I would appreciate any help.
var scrollView = new Y.ScrollView({
id: "scrollview",
srcNode: '.scrollview-item',
height: 375,
flick: {
minDistance: 10,
minVelocity: 0.3,
axis: "y"
}
});
scrollView.render();
I stumbled upon this as well, after some trial and error, I managed to get that working(note, that it is just plain scrolling, without an easing).
var DOM_MOUSE_SCROLL = 'DOMMouseScroll',
fixArgs = function(args) {
var a = Y.Array(args, 0, true), target;
if (Y.UA.gecko) {
a[0] = DOM_MOUSE_SCROLL;
// target = Y.config.win;
} else {
// target = Y.config.doc;
}
if (a.length < 3) {
// a[2] = target;
} else {
// a.splice(2, 0, target);
}
return a;
};
Y.Env.evt.plugins.mousewheel = {
on: function() {
return Y.Event._attach(fixArgs(arguments));
},
detach: function() {
return Y.Event.detach.apply(Y.Event, fixArgs(arguments));
}
};
This is the YUI mousewheel event, but it's changed a bit. The biggest issue was, that originally, either the window or document elements, which makes no sense(for example when you mousewheel over the #myelement you want that to be the returned target..)
Bellow is the code used to initialize the ScrollView and the function that handles the mousewheel event:
// ScrollView
var scrollView = new Y.ScrollView({
id: "scrollview",
srcNode: '#mycontainer',
height: 490,
flick: {
minDistance:10,
minVelocity:0.3,
axis: "y"
}
});
scrollView.render();
var content = scrollView.get("contentBox");
var scroll_modifier = 10; // 10px per Delta
var current_scroll_y, scroll_to;
content.on("mousewheel", function(e) {
// check whether this is the scrollview container
if ( e.currentTarget.hasClass('container') ) {
current_scroll_y = scrollView.get('scrollY');
scroll_to = current_scroll_y - ( scroll_modifier * e.wheelDelta );
// trying to scroll above top of the container - scroll to start
if ( scroll_to <= scrollView._minScrollY ) {
// in my case, this made the scrollbars plugin to move, but I'm quite sure it's important for other stuff as well :)
scrollView._uiDimensionsChange();
scrollView.scrollTo(0, scrollView._minScrollY);
} else if ( scroll_to >= scrollView._maxScrollY ) { // trying to scroll beneath the end of the container - scroll to end
scrollView._uiDimensionsChange();
scrollView.scrollTo(0, scrollView._maxScrollY);
} else { // otherwise just scroll to the calculated Y
scrollView._uiDimensionsChange();
scrollView.scrollTo(0, scroll_to);
};
// if we have scrollbars plugin, flash the scrollbar
if ( scrollView.scrollbars ) {
scrollView.scrollbars.flash();
};
// prevent browser default behavior on mouse scroll
e.preventDefault();
};
});
So basically that's how I managed to that, but my next challenge is to get the scrollbar work like regular scrollbar(when you drag it, the container should move correspondingly...)
Hope this helps anyone :)

Three20 Search Scope

I'm using Three20 and I've got the standard search mechanism working.
TTTableViewController* searchController = [[[TTTableViewController alloc] init] autorelease];
searchController.dataSource = [[[MyDataSource alloc] init] autorelease];
self.searchViewController = searchController;
self.tableView.tableHeaderView = _searchController.searchBar;
I'd like to use a scope. but I'm having trouble implementing it. Going through the three20 code it appears the searchdisplaycontroller is already built in. Is there a method I'm missing like
-(void)search:(NSString *)text withinScope:(NSString *)scope
How do I pull the scope from the searchdisplaycontroller? I tried using the delegate methods for the searchdisplaycontroller but the datasource isn't populating the table.
Any ideas?
Thanks,
Howie
After searching high and low, I came to the conclusion that something must be missing from the core Three20 library. I did a little snooping around and found that the UISearchDisplayDelegate methods are in TTSearchDisplayController.m and unfortunately don't incorporate the scope when they hand things off to the datasource.
Here are the modifications I made:
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)searchAfterPause {
_pauseTimer=nil;
// HOWIE MOD
if([self.searchBar.scopeButtonTitlescount])
{
NSString*scope = [[self.searchBarscopeButtonTitles]objectAtIndex:[self.searchBarselectedScopeButtonIndex]];
//NSLog(#"sending text: %# for scope: %#", self.searchBar.text, scope);
[_searchResultsViewController.dataSource search:self.searchBar.textwithinScope:scope];
}else
{
[_searchResultsViewController.dataSource search:self.searchBar.text];
}
/*
// Original
[_searchResultsViewController.dataSource search:self.searchBar.text];
*/
// /HOWIE MOD
}
and
///////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)searchDisplayController:(UISearchDisplayController*)controller
shouldReloadTableForSearchString:(NSString*)searchString {
if(_pausesBeforeSearching) {
[selfrestartPauseTimer];
} else{
// HOWIE MOD
if([self.searchBar.scopeButtonTitlescount])
{
NSString*scope = [[self.searchBarscopeButtonTitles]objectAtIndex:[self.searchBarselectedScopeButtonIndex]];
[_searchResultsViewController.dataSource search:searchString withinScope:scope];
returnYES;
} else
{
[_searchResultsViewController.dataSource search:searchString];
}
/*
// Original
[_searchResultsViewController.dataSource search:searchString];
*/
// / HOWIE MOD
}
returnNO;
}
and
///////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)searchDisplayController:(UISearchDisplayController*)controller
shouldReloadTableForSearchScope:(NSInteger)searchOption {
// HOWIE MOD
if([self.searchBar.scopeButtonTitlescount])
{
NSString*scope = [[self.searchBarscopeButtonTitles] objectAtIndex:searchOption];
[_searchResultsViewController.dataSource search:self.searchBar.textwithinScope:scope];
returnYES;
}else
{
[_searchResultsViewControllerinvalidateModel];
[_searchResultsViewController.dataSource search:self.searchBar.text];
}
/*
// Original
[_searchResultsViewController invalidateModel];
[_searchResultsViewController.dataSource search:self.searchBar.text];
*/
// / HOWIE MOD
returnNO;
}
Then I added the following to TTTableViewDataSource.h
// HOWIE MOD
- (void)search:(NSString*)text withinScope:(NSString*)scope;
// /HOWIE MOD
And the following to TTTableViewDataSource.m
// HOWIE MOD
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)search:(NSString*)text withinScope:(NSString*)scope {
}
// /HOWIE MOD
Now I can create the method - (void)search:(NSString*)text withinScope:(NSString*)scope in my datasource and it will respond accordingly as a search with scope is performed. I also enabled pausesBeforeSearching when I instantiate the search controller in my tableview controller so that it waits a couple of seconds before performing the search as a user types. This is helpful since my search is querying a server and rather than send each character as the user types, it makes more sense to let them type a few characters first.
Hope this helps.
Howie

Resources